Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / December 2007

Tip: Looking for answers? Try searching our database.

Treeview bug  ...or mystery?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Anonymous - 07 Dec 2007 12:56 GMT
Hi experts,

Using: Visual Studio 2003, .NET Framework 3.0, Treeview Class

Here's what I'm trying to do:

I've got a form with a toolbar and a treeview.
Using a dropdown button on the toolbar you can add nodes to a treeview.
When a node is added the node automatically enters the "edit" mode
(labeledit is enabled on the treeview) and disables the toolbar.
After you finish editing the node label, the code checks if that node
already exists in the treeview. If it does, it cancels the edit and
removes (or restarts edit, see code) for the node, otherwise it adds the
node and re-enables the toolbar.

Sounds simple no?

There's one big problem. If you click the node to edit it, it takes a
while until the event "BeforeNodeEdit" is fired. You can use that time
to click the toolbar so that the dropdown is shown. Now the code gets to
the "BeforeNodeEdit" event and disables the toolbar. Unfortunately, the
dropdown buttons are not disabled, so you can still click "Add node". If
you do that the treeview gets all confused and the result is either an
access violation or a complete non-responding treeview.

I must be doing something really strange, but I can't find what it is.

Code to reproduce:

Create a new C# windows forms application. Add a toolstrip to it and add
a treeview to it. Keep the default names.

Now paste in the code below, run the application and perform the
following tasks:

Use the toolbar to add a node. Leave the node name as "Node".
Add another node, now edit the name to "Node2"
Add another node, now edit the name to "Node3"
Now click "Node2" to edit the node.
You can see the toolbar gets disabled once the labeledit starts.
Do it again, but now click the toolbar right after you clicked the node
to edit.
After a short while you will see that the toolbar is getting disabled,
but "Add node" is still enabled. Click it.

**CRASH** What am I doing wrong??? Is there a way around this? Help...

Code:

private void Form1_Load(object sender, EventArgs e)
{
    ToolStripDropDown tsDropDown = new ToolStripDropDown();
    ToolStripDropDownButton tsDropDownButton = new
ToolStripDropDownButton("Items");
    tsDropDownButton.DropDown = tsDropDown;

    ToolStripItem tsItem = toolStrip1.Items.Add("Add item", null, new
EventHandler(this.OnAddItem));
    tsDropDown.Items.Add(tsItem);
    toolStrip1.Items.Add(tsDropDownButton);

    treeView1.LabelEdit = true;
}

void OnAddItem(object sender, EventArgs e)
{
    TreeNode node = treeView1.Nodes.Add("Node");
    node.BeginEdit();
}

private void treeView1_BeforeLabelEdit(object sender,
NodeLabelEditEventArgs e)
{
    toolStrip1.Enabled = false;
}

private void treeView1_AfterLabelEdit(object sender,
NodeLabelEditEventArgs e)
{
    string nodeText = ((null == e.Label) ? e.Node.Text : e.Label);
    TreeNode[] nodes = treeView1.Nodes.Find(nodeText, false);
    if ((nodes.Length>0) && (nodes[0] != e.Node))
    {
        e.CancelEdit = true;
        // This results in an access violation after return of this
function
        e.Node.TreeView.Nodes.Remove(e.Node);

        // This results in a non-responsive treeview after return of
this function
        //e.Node.BeginEdit();
    }
    else
    {
        e.Node.Name = nodeText;
    }
    toolStrip1.Enabled = true;
}
Kevin Spencer - 10 Dec 2007 11:54 GMT
Use the MouseDown event of the TreeNode to temporarily disable the Toolbar.

Signature

HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

> Hi experts,
>
[quoted text clipped - 94 lines]
>     toolStrip1.Enabled = true;
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.