treeview node is highlighted even i did not right click on the node

后端 未结 4 581
孤独总比滥情好
孤独总比滥情好 2021-01-28 11:05

I am working on a winform and on my UI there is a treeview, I found that the treenode will be highlighted even I did not click on the node by right mouse (eg, Node1 will be high

4条回答
  •  误落风尘
    2021-01-28 12:04

    I have found another method to prevent the node to be highlight when the user not click on the node, and I only set the BackColor and ForeColor for each of the node when adding it to the tree

    newNode.BackColor = treeview1.BackColor;
    newNode.ForeColor = treeview1.ForeColor;
    treeview1.Nodes.Add(newNode);
    

    Then in the MouseDown event, set the SelectedNode property as following

     private void treeView1_MouseDown(object sender, MouseEventArgs e)
     {
            TreeNode Node = treeView1.GetNodeAt(e.Location);
            if (Node != null && Node.Bounds.Contains(e.Location))
                treeView1.SelectedNode = Node;
            else
                treeView1.SelectedNode = null;
     } 
    

提交回复
热议问题