Windows Develop Bookmark and Share   
 index > Windows Forms General > Problem with treeView.NodeMouseClick Event
 

Problem with treeView.NodeMouseClick Event

Hi to all !

I have a problem with the NodeMouseClick Event of the treeView control.

Example:
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node.IsExpanded)
{
//Code to load the TreeView goes here
}
}

When I click the Root Node, the event is correctly hit, the TreeView is populated and expanded.
I click on the TreeView control again, the event is correctly hit and the TreeView collapse.

From now the NodeMouseClick event is never hit again ....

What's the problem ? ?

N.
Nazza  Thursday, March 29, 2007 3:17 PM
Bug in the native Windows Treeview component., the framework can't fix it. Modifying the nodes collection in any of the mouse events is asking for trouble. Your particular problem is caused by it not seeing the MouseUp event. The (crummy) workaround I use is to start a 1 msec timer and update the treeview in the timer's Tick event.
nobugz  Friday, March 30, 2007 11:05 AM

Hi,

I can guess,it's not the problem with Tree control,it's the code written inside Node_mouseClick(might be making the application not responding) or some other thing.How can u say that this event is not getting fired?

If this event is not getting fired means,processor is busy in processing some otherthing in the same applicaiton,i guess.

Here i'm posting my simple code snippet,everytime i'm getting the NodeMouseClick event fired,based on which i said the above things.

My form just has treeview control.That's it.U can try this.


private void Form1_Load(object sender, EventArgs e)
{
TreeNode tnRoot = treeView1.Nodes.Add("Root");
TreeNode tnChild = tnRoot.Nodes.Add("Child1");
tnChild.Nodes.Add(
"child11");
}
private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);
}


Thanx,

Ch.T.Gopi Kumar.

TilakGopi  Thursday, March 29, 2007 3:38 PM
Hi,
I put a breakpoint on the NodeMouseClick event. It is fired the first time I click the Node and when i collapse it. But it's not fired again.

The processor seems to be free, because the application is not freezed.

Mmmmhhhhh..........perhaps the problem occurs because my TreeView is dinamically populated ? ? I'll do some test on this way.

Thank you
Nazza  Thursday, March 29, 2007 3:57 PM

Hi,

dynamically populated in the sense,how did u populate?let me try that case also?

Thanx,

Ch.T.Gopi Kumar.

TilakGopi  Thursday, March 29, 2007 4:06 PM
Hi,
inside the treeView1_NodeMouseClick event code, I call a stored procedure to dinamically add nodes to the control. Maybe in this way (updating the nodes collection) the control loses link to the event handler ? ?
Nazza  Friday, March 30, 2007 8:36 AM
Hi,
I finally found the cause !

The second time I expand my treeView, the root node is moved to the upper border of the control (it contains many items) to expand.
So the mouse pointer loses the node and the event is not fired.

Try this code:

private void Form1_Load(object sender, EventArgs e)
{
TreeNode tnRoot = treeView1.Nodes.Add("Root");
TreeNode tnRoot2 = tnRoot.Nodes.Add("Root_2");
TreeNode tnChild = tnRoot2.Nodes.Add("Child1");
}

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
MessageBox.Show(e.Node.Text);

if (e.Node.Text=="Root_2")
{
e.Node.Nodes.Clear();

for (int iCounter = 0; iCounter < 100; iCounter++)
{
e.Node.Nodes.Add(iCounter.ToString());
}
}
}

The second time you try to expand Root_2, the event won't fire !

Maybe a VisualStudio 2005 bug ? ?

I'll send a feedback to Microsoft site....

Thank you,
Nazareno
Nazza  Friday, March 30, 2007 10:24 AM
Bug in the native Windows Treeview component., the framework can't fix it. Modifying the nodes collection in any of the mouse events is asking for trouble. Your particular problem is caused by it not seeing the MouseUp event. The (crummy) workaround I use is to start a 1 msec timer and update the treeview in the timer's Tick event.
nobugz  Friday, March 30, 2007 11:05 AM
Hi,
in this case I'm trying to use the BeforeExpand event instead of the NodeMouseClick.

Thank you to all !
Nazareno

Nazza  Friday, March 30, 2007 11:20 AM

You can use google to search for other answers

Custom Search

More Threads

• Using the default drag-copy icon
• How to built custom DataGridViewColumnHeaderCell ?
• How do I get a flash page to work in a webBrowser object?
• Copying clipboard RTF data to excel sheet and visio
• Able to use ImageList with different size?
• Help !!!!
• Pop-up Blocker Settings
• how can i set a column's backcolor of ListView control(c#)
• Application settings
• help!