473,390 Members | 1,334 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,390 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 2107
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.