开发者

Setting TreeView ForeColor using C#

开发者 https://www.devze.com 2023-01-24 19:52 出处:网络
I have a treeview that when the user interacts with individual nodes, the colours change. The code is:

I have a treeview that when the user interacts with individual nodes, the colours change. The code is:

treeview.selectednode.forecolor = color.red;

When the user presses a button, I want the whole set of nodes to change to black for example. So I code as such:

treeview.forecolor = color.black;

It works fine, except for the nodes that I have previ开发者_Go百科ously changed to red. Is there a way to get around this?


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);
    }
}


Keep a reference to the previously selected node, turn it into black whenever you change treeview to black.

0

精彩评论

暂无评论...
验证码 换一张
取 消