ApsSanj
ApsSanj

Reputation: 579

Treeview highlights a random node after node click event

I have a treeview with four hierarchical levels. When click on some treenode, relevant data will be loaded in a listview. Upon click on some treenodes, I am experiencing an issue with some random node highlighting instead of the clicked treenode after the treeview node click event got executed. Most importantly, this issue doesn't happen at every click. But it is happening frequently, like 1/5 times. It is just a highlight, a node click doesn't occur.

Below is the characteristics of the tree nodes with the issue

Below is my treeview node click event.

    private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        var hitTest = e.Node.TreeView.HitTest(e.Location);
        if (hitTest.Location != TreeViewHitTestLocations.PlusMinus)
        {
            treeView1NodeMouseClick(sender, e);
            treeView1.Refresh();
        }
    }

With debugging, identified that this random selection happens after the treeView1_NodeMouseClick finishes executing. Tried checking the treeView1_BeforeSelect event, the selection happens after the node click event executed. I have set this.treeView1.HideSelection = false;. Tried treeview1.Refresh();. None of them worked.

Anybody got idea why this is happening and how to fix it?

Thanks in advance!

Upvotes: 0

Views: 53

Answers (1)

ApsSanj
ApsSanj

Reputation: 579

Found the issue. I was running the treeview node sorter for mouse clicked node's child nodes upon each treeView1_NodeMouseClick event. But in this instance, those mouse clicked nodes does not contain child nodes. That treeview node sorter is the one who highlights some other node.

Added logic to only run the node sorter when the selecting node contain child nodes.

    if (selectedNode.Nodes.Count > 0)
    {
        //Sort only the child nodes of the selected parent node ; parent node should passe to the NodeSorter
        selectedNode.TreeView.TreeViewNodeSorter = new NodeSorter(selectedNode);
        selectedNode.TreeView.Sort();
    }

Keeping this issue without removing it as somebody might get similar issue which is unable to easily track by debugging.

Upvotes: 0

Related Questions