AlucardHellSing

I have used the mouse down event and GetNodeAt method to enable user to right click on a node to being selected. However I found some 'side effects'....whenever I click outside the display treenodes but within the control, it selects according to the row which i have click.This is an undesirable result which I dont want. Is there any solution to it

Thanks.

Regards

Alu




Re: Windows Forms General Tree View Right Click

Sushil B

Hi Alu,

Use code similar to the following to resolve the mentioned issue:

private void treeView1_MouseUp(object sender, MouseEventArgs e)

{

// Show menu only if the right mouse button is clicked.

if (e.Button == MouseButtons.Right)

{

// Point where the mouse is clicked.

Point p = new Point(e.X, e.Y);

// Get the node that the user has clicked.

TreeNode node = treeView1.GetNodeAt(p);

if (node != null)

{

MessageBox.Show(node.Text);

}

}

}

You can utilize the MouseUp or MouseDown event, however MouseUp is ideal in this scenario.

Hope this helps.

-Sushil






Re: Windows Forms General Tree View Right Click

AlucardHellSing

Hi thanks for the code. Unfortunately even if i click out of the tree node (within the control), i still get the node!= null which triggers the Message Box Sad

Any idea what could be wrong

Thanks.

Regards

Goh






Re: Windows Forms General Tree View Right Click

.Binny

consume the NodeMouseClick event of the treeview. this event fires when the node within the tree is clicked. a node has a Bounds property which we can use to determine if the mouse click location was within it.

ex:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e) {

if (e.Button == MouseButtons.Right) {

if (e.Node.Bounds.Contains(e.Location)) {

MessageBox.Show(e.Node.Text);

}

}

}





Re: Windows Forms General Tree View Right Click

AlucardHellSing

Unfortunately that doesnt work either Bound.Contains is always true even if I click all over the place and out of the tree....

Regards

Goh






Re: Windows Forms General Tree View Right Click

JeroGrav

Check out TreeView.HitTest().

I haven't tried it myself but it looks like it returns more than just the node it found but also has where the in the node the hit was.

It may give you more of the info you need.

Regards






Re: Windows Forms General Tree View Right Click

.Binny

Goh,

Bounds property of the TreeNode should work. Make sure that the TreeNode & the Location property u are using is what was supplied in the TreeNodeMouseClickEventArgs of the NodeMouseClick event handler.





Re: Windows Forms General Tree View Right Click

nobugz

The Bounds property check will always evaluate to True, the TreeView control has already done that. It wouldn't generate an event otherwise. Can you explain why the built-in NodeMouseClick event isn't good enough





Re: Windows Forms General Tree View Right Click

AlucardHellSing

I tried the nodemouseclick event. But still when i click out of the node along side the control. That triggers this mouse click.

Regards

Alu