473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 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 2103
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Brian Smith | last post by:
Is there a way to avoid the default action of TreeNode expansion/contraction caused by double click? I can add an event handler to pop up my properties dialog on double click, but it has the...
1
by: charliewest | last post by:
I would like to implement the following functionality: When a user taps a (child) treeNode, an event fires which allows me to get the selected treeNode's TAG object, determine it's values, and...
2
by: tmaster | last post by:
I can detect a right click on my treeview, but SelectedNode.Index points to the last node that was left-clicked. Is there a way for a right-click event of a treeview to update the...
1
by: Patrick | last post by:
This may be a simple question but how can I make the tree change the selected node... on a right click .. in addition to the left click? What I want is to filter my context menu based on the type...
2
by: worzel | last post by:
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...
3
by: Matthew Woods | last post by:
Hi, does anyone know how to select a treenode with the first click? the first click on a treeview will highlight the node under the mouse when it was clicked BUT that node isn't actually selected...
2
by: garyusenet | last post by:
I could do with something similiar, can you tell me if you think this would work for me, and if there's any advantage in working with controls this way than how I currently am. At the moment...
3
by: sugee | last post by:
hi, I have a treeview when rightclicked a context menu should appear. but first time wen i right click the context menu of that node(ie, add,delete) appears whereas when i right click another...
2
by: Bry | last post by:
I have a context menu which is shared between a TreeView and ListView control (both controls show the same information, similar to how Windows Explorer works, so it makes sense to use the same menu...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.