some questions about treeview!!

I am trying to make a directory tree in c# using a tree view.And i want the node to expand only when any node is double clicked.For doubleclick event i used the following code:

this.treeView1.DoubleClick +=new System.EventHandler(treeView1_DoubleClick);

private void treeView1_DoubleClick(object sender, EventArgs e){
if (treeView1.SelectedNode !=null){
//expand the selected node  
}
  }

but the problem is this event works if i click in any other area of the control
if i single click a treenode, the node gets selected and now if i double clicked in any other part of the control still it will works
Is there any way to avoid this
And is there any way that i can deselect any selected node


Answer this question

some questions about treeview!!

  • Andrei Asayonak

    Can u explain that in detail with code. I couldnot get that.
    Ok i want to explain my problem in detail.I have made an application with a text editor and a directory viewer. Some portion of the form is occupied by a directory viewer where user can select the files.What i want to do is that whenever a user double clicked any file from the directory viewer i want to open that file in my text editor.How can i use the doubleclick event to know which file is selected

  • Med Bouchenafa

    There is a TreeNode from Point method of the treeview. You should use this instead of the SelectedNode. Then you check if the treenode returned by that method is null - if itis skip it - if its not then expand/collapse. Not sure if this will solve all of your other problems but this is something that is wrong with your code.

    I would also encourage you just to let the TreeView use its default collapsing / expanding rules as this is most familiar to users.

  • some questions about treeview!!