Customizing TreeView ForeColor Using C #

I have a tree, when the user interacts with individual nodes, the colors change. The code:

treeview.selectednode.forecolor = color.red;

When the user clicks the button, I want the entire set of nodes to change to black, for example. Therefore, I code as such:

treeview.forecolor = color.black;

It works fine, except for those nodes that I previously changed to red. Is there any way around this?

+3
source share
2 answers

Use this recursive function:

private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
    (sender as TreeView).SelectedNode.ForeColor = Color.Red;
}

private void button1_Click(object sender, EventArgs e)
{
    foreach (TreeNode tn in treeView1.Nodes)
    {
        tn.ForeColor = Color.Blue;
        ColorNodes(tn);
    }
}

private void ColorNodes(TreeNode t)
{
    foreach (TreeNode tn in t.Nodes)
    {
        tn.ForeColor = Color.Blue;
        ColorNodes(tn);
    }
}
+2
source

Keep a link to the previously selected node, turn it black when you change the tree to black.

0
source

Source: https://habr.com/ru/post/1774323/


All Articles