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



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.PointToClient(Control.MousePosition));
if (this.treeView1.SelectedNode != null)
{
this.contextMenu1.MenuItems.Clear();

this.contextMenu1.MenuItems.Add(this.treeView1.SelectedNode.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.
>
>
>
.



Relevant Pages

  • Re: I want a right click to cause a treenode to become selected, how?
    ... >> Okay, new to win forms; I have a treeview with several root nodes, each ... I also havea context ... >> This happening becuase the right click on said child node does not cause ... the parent node remains the selected node! ...
    (microsoft.public.dotnet.general)
  • howdo i have a right click cause a tree node to become selected?
    ... I have a treeview with several root nodes, each ... right click on a childNode I still get the context menu options for the ... This happening becuase the right click on said child node does not cause it ... the parent node remains the selected node! ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • I want a right click to cause a treenode to become selected, how?
    ... I have a treeview with several root nodes, each ... right click on a childNode I still get the context menu options for the ... This happening becuase the right click on said child node does not cause it ... the parent node remains the selected node! ...
    (microsoft.public.dotnet.general)
  • Re: Problem with AfterSelect event in treeview
    ... > I have a form with a button, treeview and listbox. ... > after that select some child node in the treeview, ... > How can I prevent this first afterselect event from happening, ... > I do not want to display listbox data for this parent node first if I ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Problem with AfterSelect event in treeview
    ... I have a form with a button, treeview and listbox. ... after that select some child node in the treeview, ... I do not want to display listbox data for this parent node first if I ...
    (microsoft.public.dotnet.framework.windowsforms)