Select correct treenode with first click

  • Thread starter Thread starter Matthew Woods
  • Start date Start date
M

Matthew Woods

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 until you click on it
again - very annoying property. i know i have to handle an event to set the
selected node to be the highlighted node but i have no idea how.

Thanks for any help,

Matt
 
try using the MousDown or MouseUp event, whichever proves more reliable.
e.g.:
private void treeView1_MouseDown(object sender, MouseEventArgs e)
{
try
{
TreeNode node =
this.treeView1.GetNodeAt(PointToClient(Cursor.Position));
this._curSelectedNode = node;
}
catch
{}
}


// _curSelectedNode is just a class-level field to hold the selection
--Peter
 
Thanks for your help but it doesn't work. My code is as follows:

1 private void tvServerProducts_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
2 {
3 try
4 {
5 TreeNode tn =
tvServerProducts.GetNodeAt(PointToClient(Cursor.Position));
6 tvServerProducts.SelectedNode = tn;
7 }
8 catch {}
9 }

but if i put a breakpoint at line 6 then it tells me tn is undefined. the
same thing happens in the MouseUp event.
 
Ok, it does work, but only if you click on the node line or the image. if
you click on the text then it doesn't return any node.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top