vb.net 更改树视图子节点颜色文本颜色和图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23317222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:28:14  来源:igfitidea点击:

Change treeview sub node colour text colour and image

vb.nettreeview

提问by elmonko

I'm using the below to search for a node named "Archive" and then change its colour to red and image index.

我正在使用下面的搜索名为“Archive”的节点,然后将其颜色更改为红色和图像索引。

Dim d As String = "Archive"
            For i = 0 To tvProgress.Nodes.Count - 1
                If tvProgress.Nodes(i).Text = d Then
                    tvProgress.Nodes(i).ForeColor = Color.Red
                    tvProgress.Nodes(i).ImageIndex = 1
                End If
            Next

As you see from the below image the node "Archive" has some structure underneath. I would also like to change the colour and image index of these as well. These are not a static node name like "Archive" so I can't simply repeat the process.

正如您从下图中看到的,节点“存档”下面有一些结构。我还想更改这些的颜色和图像索引。这些不是像“存档”这样的静态节点名称,所以我不能简单地重复这个过程。

There are also other nodes in the treeview that need to be left as the default Blue Folder, Black Text

treeview中还有其他节点需要留作默认的Blue Folder, Black Text

Is this possible?

这可能吗?

enter image description here

在此处输入图片说明

采纳答案by Piratica

You should be able to use this code, just set dto the node you want to search for, and pto anything you want to preserve.

您应该能够使用此代码,只需将其设置d为您想要搜索的节点,以及p您想要保留的任何内容。

'This stores every node in the TreeView
Dim allNodes As New List(Of TreeNode)

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'the node you want to search for
    Dim d As String = "Archive"
    'the node you want to preserve
    Dim p As String = "Preserve"
    CallRecursive(TreeView1)
    For Each n As TreeNode In allNodes
        If n.Text = d Then
            n.ForeColor = Color.Red
            n.ImageIndex = 1
        ElseIf Not n.Text = p Then
            Dim path As String = n.FullPath
            Dim l As List(Of String) = path.Split("\").ToList()
            If l.Contains(d) Then
                If l.IndexOf(n.Text) > l.IndexOf(d) Then
                    n.ForeColor = Color.Red
                    n.ImageIndex = 1
                End If
            End If
        End If
    Next
End Sub

Private Sub GetRecursive(ByVal n As TreeNode)
    allNodes.Add(n)
    Dim aNode As TreeNode
    For Each aNode In n.Nodes
        GetRecursive(aNode)
    Next
End Sub

Private Sub CallRecursive(ByVal aTreeView As Windows.Forms.TreeView)
    allNodes.Clear()
    Dim n As TreeNode
    For Each n In aTreeView.Nodes
        GetRecursive(n)
    Next
End Sub

The procedure used to get every node in the TreeViewis called recursiveprocedure, which basically means that GetRecursive()will call itself until it has been through every node in your TreeView. Thanks to this, this code will go through any TreeView, regardless of depth.

This is the TreeViewI used to test this code, beforethe code is run:

Before:

And afterthe code is run:

And after:

I hope this helps, any problems and I will try and help.

用于获取在每个节点的过程TreeView称为递归程序,这基本上意味着GetRecursive()会调用自身,直到它已通过每个节点在你的TreeView。多亏了这一点,这段代码将通过 any TreeView,无论深度如何。

这是TreeView用来测试这个代码,我之前是运行代码:

前:

之后运行的代码:

之后:

我希望这有助于任何问题,我会尝试和帮助。



Edit:

编辑:

If you just want to format allnodes under "Archive", use this slightly modified code:

如果您只想格式化“存档”下的所有节点,请使用此稍微修改的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'the node you want to search for
    Dim d As String = "Archive"
    CallRecursive(TreeView1)
    For Each n As TreeNode In allNodes
        If n.Text = d Then
            n.ForeColor = Color.Red
            n.ImageIndex = 1
        Else
            Dim path As String = n.FullPath
            Dim l As List(Of String) = path.Split("\").ToList()
            If l.Contains(d) Then
                If l.IndexOf(n.Text) > l.IndexOf(d) Then
                    n.ForeColor = Color.Red
                    n.ImageIndex = 1
                End If
            End If
        End If
    Next
End Sub

Private Sub GetRecursive(ByVal n As TreeNode)
    allNodes.Add(n)
    Dim aNode As TreeNode
    For Each aNode In n.Nodes
        GetRecursive(aNode)
    Next
End Sub

Private Sub CallRecursive(ByVal aTreeView As Windows.Forms.TreeView)
    allNodes.Clear()
    Dim n As TreeNode
    For Each n In aTreeView.Nodes
        GetRecursive(n)
    Next
End Sub