none
Deselect treeNode in TreeView RRS feed

  • Question

  • Hi,
    I have the following problem (I think it's very easy but I am stuck on it):
    I have a treeView with TreeNodes and I want that the treeView.SelectedNode is set to either the selected node, or, if the user clicks on a place on the treeview, where no node is, is set to null.
    I have two eventhandlers:

    this.accountGroupTreeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.accountGroupTreeView_AfterSelect);
                this.accountGroupTreeView.MouseUp += new System.Windows.Forms.MouseEventHandler(this.accountGroupTreeView_MouseUp);
               

      private void accountGroupTreeView_AfterSelect(object sender, TreeViewEventArgs e)
            {
                if (e.Node.Parent != null)
                {
                    this.selectedGroupNode = e.Node.Parent;
                }
                else
                {
                    this.selectedGroupNode = e.Node;
                   
                }
            
            }

            private void accountGroupTreeView_MouseUp(object sender, MouseEventArgs e)
            {
                if(e.Button == MouseButtons.Right)
                {
                     // Select the clicked node
                     this.accountGroupTreeView.SelectedNode = this.accountGroupTreeView.GetNodeAt(e.X, e.Y);
                     if (this.accountGroupTreeView.SelectedNode != null)
                     {
                         myContextMenuStrip.Show(this.myContextMenuStrip, e.Location);
                     }
               
                }
                if (e.Button == MouseButtons.Left)
                {
                    this.accountGroupTreeView.SelectedNode = this.accountGroupTreeView.GetNodeAt(e.X, e.Y);
                }
            }



    But taht doesn't work, I always have something different from null.
    And if I delete the "AfterSelect" event handler, it is always null.
    Any ideas?


    Monday, October 2, 2006 2:24 PM

All replies

  • e.X and e.Y is the screen/monitor coordinate. So insert something like this.

    Point pnt = this.accountGroupTreeView.PointToClient(e.X, e.Y); // you need the coordinate relative to the treeview control

    ...GetNodeAt(pnt);

    Monday, October 2, 2006 8:11 PM
  • Hi,

    Is your problem solved?

    Bhanu.

    Monday, October 2, 2006 10:39 PM