471,349 Members | 1,247 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,349 software developers and data experts.

I want a right click to cause a treenode to become selected, how?

Can't suss this out for the life of me, googling of no help either.

Okay, new to win forms; I have a treeview with several root nodes, each
having a single level of child nodes underneath. I also havea context
(popup) menu which, what I want, is for it to pop up with certain options
(menuItems) when a root treeNode is selected and different options when a
child node is selected. It is all working well except for one problem - if I
right click on a childNode I still get the context menu options for the
parent (vica versa too)

This happening becuase the right click on said child node does not cause it
to be selected, the parent node remains the selected node! But I do not want
people to have to first left click to select the child node and then do the
right click to bring up the context menu with the appropriate options - most
apps I seee don't force this as a requirement.

Assuming I explained that all well enough, anybody got any ideas on what I
need to be doing??

TIA.

Andy.
Aug 18 '05 #1
2 1994
You need to change the selected node of the treeview before showing the
context menu. The following code should do the trick:

private void contextMenu1_Popup(object sender, System.EventArgs e)
{
this.treeView1.SelectedNode =
this.treeView1.GetNodeAt(this.treeView1.PointToCli ent(Control.MousePosition));
if (this.treeView1.SelectedNode != null)
{
this.contextMenu1.MenuItems.Clear();

this.contextMenu1.MenuItems.Add(this.treeView1.Sel ectedNode.Text);
}
}

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk
"worzel" wrote:
Can't suss this out for the life of me, googling of no help either.

Okay, new to win forms; I have a treeview with several root nodes, each
having a single level of child nodes underneath. I also havea context
(popup) menu which, what I want, is for it to pop up with certain options
(menuItems) when a root treeNode is selected and different options when a
child node is selected. It is all working well except for one problem - if I
right click on a childNode I still get the context menu options for the
parent (vica versa too)

This happening becuase the right click on said child node does not cause it
to be selected, the parent node remains the selected node! But I do not want
people to have to first left click to select the child node and then do the
right click to bring up the context menu with the appropriate options - most
apps I seee don't force this as a requirement.

Assuming I explained that all well enough, anybody got any ideas on what I
need to be doing??

TIA.

Andy.

Aug 18 '05 #2
You are a champion! Thanks for that - it was just what I needed.

I did find a half solution by using the mousedown handler and using e.x and
e.y business to get and preselect the node, had no idea about this
PointToClient stuff - much better.

Andy
"Jakob Christensen" <jc*@REMOVEpension.dk> wrote in message
news:5A**********************************@microsof t.com...
You need to change the selected node of the treeview before showing the
context menu. The following code should do the trick:

private void contextMenu1_Popup(object sender, System.EventArgs e)
{
this.treeView1.SelectedNode =
this.treeView1.GetNodeAt(this.treeView1.PointToCli ent(Control.MousePosition));
if (this.treeView1.SelectedNode != null)
{
this.contextMenu1.MenuItems.Clear();

this.contextMenu1.MenuItems.Add(this.treeView1.Sel ectedNode.Text);
}
}

HTH, Jakob.

--
http://www.dotninjas.dk
http://www.powerbytes.dk
"worzel" wrote:
Can't suss this out for the life of me, googling of no help either.

Okay, new to win forms; I have a treeview with several root nodes, each
having a single level of child nodes underneath. I also havea context
(popup) menu which, what I want, is for it to pop up with certain options
(menuItems) when a root treeNode is selected and different options when a
child node is selected. It is all working well except for one problem -
if I
right click on a childNode I still get the context menu options for the
parent (vica versa too)

This happening becuase the right click on said child node does not cause
it
to be selected, the parent node remains the selected node! But I do not
want
people to have to first left click to select the child node and then do
the
right click to bring up the context menu with the appropriate options -
most
apps I seee don't force this as a requirement.

Assuming I explained that all well enough, anybody got any ideas on what
I
need to be doing??

TIA.

Andy.

Aug 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Brian Smith | last post: by
1 post views Thread by Patrick | last post: by
3 posts views Thread by Matthew Woods | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.