Uros Calakovic
Uros Calakovic

Reputation:

Expand Windows Forms TreeView node without selecting it

When I expand a TreeView node by clicking on the plus sign right to it, the node gets selected. How can I avoid this? I want to be able to expand nodes without changing the selected node (like in RegEdit.exe, for example), and only change selection when the node text is clicked .

(Forgive me for what seems to be a basic question - I did search around, but found nothing. Any pointers or links are welcome.)

Upvotes: 4

Views: 2186

Answers (2)

zybroxz
zybroxz

Reputation: 713

A bit late to the party with this.

You can use a Hit Test.

    private void myTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {

        TreeViewHitTestInfo info = myTreeView.HitTest(e.Location);

        if (info.Location == TreeViewHitTestLocations.Label)
        {
            TreeNode node = myTreeView.GetNodeAt(e.Location);

            //do something
        }
    }

This will only select the node if the label is clicked.

Hope this helps.

Upvotes: 0

Sergey
Sergey

Reputation:

I believe there is a BeforeSelect event you can tap into, which should allow you to cancel node selection if the selected node has children.

private void MyTreeView_BeforeSelect(object sender, TreeViewCancelEventArgs e)
{
 If (nodeWithChildren) e.Cancel = True
}

Upvotes: 1

Related Questions