Windows Develop Bookmark and Share   
 index > Windows Forms General > How to detect which TreeNode was Double Clicked?
 

How to detect which TreeNode was Double Clicked?

How do I detect which TreeNode in my TreeView was DoubleClicked?

I can detect which TreeNode was right clicked using this code (where treeMain is the TreeView control):

private void treeView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
TreeNode oTreeNode = this.treeMain.GetNodeAt(e.X, e.Y);
if(oTreeNode != null)
{
// display context menu
treeMain.SelectedNode = oTreeNode;
this.contextMenuTree.Show(this.treeMain, new Point(e.X, e.Y));
}
}
}

...but the DoubleClick event has different arguments passed to it so e.X and e.Y won't compile.

Thanks in advance,

Chiz.
MigrationUser 1  Wednesday, June 02, 2004 6:41 PM
I see from the documentation that you need to be using the Mouse_Up or Mouse_Down to get the location of the mouse pointer.  So what you'll probably have to do is save the mouse coordinates in some form-level variables in the Mouse_Up event, then use them when the DoubleClick event gets raised.  Funny that the Click and DoubleClick events receive standard EventArgs and not MouseEventArgs.  I'm sure Microsoft had a good reason though.

Tony
MigrationUser 1  Thursday, June 03, 2004 7:25 AM
Here is a code example I just saw. You can use Control.MousePosition to get the mouse coordinates at any time. However, that is screen coordinate and not relative to your control. Hence the extra call in here to convert the point to client coordinates.


private void OnDoubleClick(EventArgs e)
{

TreeNode doubleClickedNode = treeView1.GetNodeAt(
         treeView1.PointToClient(Control.MousePosition));
}

MigrationUser 1  Thursday, June 03, 2004 4:17 PM

You can use google to search for other answers

Custom Search

More Threads

• This forum IS NOT for Windows troubleshooting questions
• WYSIWYG component
• Can Windows Client delete my A record on DNS Server when is Log Off?
• how to invoke this method
• Balloon tip in .net
• tab and IsInputKey
• Display Live Video inside a form
• browsing open forms
• find untility-find multi-word string
• [TextBox] how to get the text of a data bounded Textbox?