Connecting Tech Pros Worldwide Forums | Help | Site Map

Getting the node the user right clicked on

Hylton
Guest
 
Posts: n/a
#1: Nov 17 '05
Hi

In a treeview How do I get the node that the user right clicked on. I need
to modify the contecxt menu based on the node





Nicholas Paldino [.NET/C# MVP]
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Getting the node the user right clicked on


Hylton,

In .NET 2.0, you can call the HitTest method on the TreeView to get a
TreeViewHitTestInfo class instance which will have the node that the point
passed to the HitTest method points to (if there is one).

If not in .NET 2.0, you will have to call SendMessage through the
P/Invoke layer, passing the TV_HITTESTINFO message to get the location of
the the node at the specified coordinates.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

"Hylton" <Hylton@discussions.microsoft.com> wrote in message
news:A30A65CE-A625-4265-B20C-777F1919DA65@microsoft.com...[color=blue]
> Hi
>
> In a treeview How do I get the node that the user right clicked on. I need
> to modify the contecxt menu based on the node
>
>
>
>[/color]


Hylton
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Getting the node the user right clicked on


Found another solution too.

private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
if(MouseButtons.Right)
{
TreeNode node = treeView1.GetNodeAt(e.X, e.Y);
....
}
}

"Nicholas Paldino [.NET/C# MVP]" wrote:
[color=blue]
> Hylton,
>
> In .NET 2.0, you can call the HitTest method on the TreeView to get a
> TreeViewHitTestInfo class instance which will have the node that the point
> passed to the HitTest method points to (if there is one).
>
> If not in .NET 2.0, you will have to call SendMessage through the
> P/Invoke layer, passing the TV_HITTESTINFO message to get the location of
> the the node at the specified coordinates.
>
> Hope this helps.
>
>
> --
> - Nicholas Paldino [.NET/C# MVP]
> - mvp@spam.guard.caspershouse.com
>
> "Hylton" <Hylton@discussions.microsoft.com> wrote in message
> news:A30A65CE-A625-4265-B20C-777F1919DA65@microsoft.com...[color=green]
> > Hi
> >
> > In a treeview How do I get the node that the user right clicked on. I need
> > to modify the contecxt menu based on the node
> >
> >
> >
> >[/color]
>
>
>[/color]
The Crow
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Getting the node the user right clicked on


declare a global variable holding the last selected node.

private TreeNode currentNode;

handle the AfterSelect event of the treeview.



private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)

{

currentNode = e.Node;

}



handle the doubleclick and keydown events to capture the enter key.



private void treeView1_DoubleClick(object sender, System.EventArgs e)

{

MessageBox.Show(currentNode.Text);

}



private void treeView1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)

{

if(e.KeyCode == Keys.Enter)

{

MessageBox.Show(currentNode.Text);

}

}



note that doubleclick behaviour will be not desired if the mouse is not
clicked a node.


The Crow
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Getting the node the user right clicked on


sorry i didnt see you wanted to capture right click..


Closed Thread


Similar C# / C Sharp bytes