HighTechTalks DotNet Forums  

Treeview bug ...or mystery?

Dotnet Framework (WinForms Controls) microsoft.public.dotnet.framework.windowsforms.controls


Discuss Treeview bug ...or mystery? in the Dotnet Framework (WinForms Controls) forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
Anonymous
 
Posts: n/a

Default Treeview bug ...or mystery? - 12-07-2007 , 07:56 AM






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;
}

Reply With Quote
  #2  
Old   
Kevin Spencer
 
Posts: n/a

Default Re: Treeview bug ...or mystery? - 12-10-2007 , 06:54 AM






Use the MouseDown event of the TreeNode to temporarily disable the Toolbar.

--
HTH,

Kevin Spencer
Chicken Salad Surgeon
Microsoft MVP

"Anonymous" <who (AT) cares (DOT) com> wrote

Quote:
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;
}



Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.