vb.net - 更改treeview子节点颜色文本颜色和图像

标签 vb.net treeview

我使用下面的内容来搜索名为“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

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

TreeView 中还有其他节点需要保留为默认的蓝色文件夹、黑色文本

这可能吗?

enter image description here

最佳答案

您应该能够使用此代码,只需将 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

用于获取TreeView中每个节点的过程称为递归过程,这基本上意味着GetRecursive()将调用自身,直到它已经遍历了 TreeView 中的每个节点。因此,此代码将遍历任何 TreeView,无论深度如何。

这是我用来测试此代码的 TreeView代码运行之前:

Before:

代码运行后:

And after:

我希望这会有所帮助,有任何问题我都会尽力提供帮助。


编辑:

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

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

关于vb.net - 更改treeview子节点颜色文本颜色和图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23317222/

相关文章:

c# - 自定义控件上的单击事件未触发(从包含表单分配的事件)

c# - C#中的递归和匿名方法

python - 如何完全改变 tkinter.ttk Treeview 的背景颜色

vb.net - 使用 VB.NET 重置打印机

vb.net - 是否可以使用字符串键/值对初始化New System.Collections.Generic.Dictionary?

c# - 如何通过鼠标和按键事件确定 TreeView 上的节点

python - 如何对包含两个 ttk Treeviews 的 ttk Notebook 进行网格化,以便它扩展以填充其指定的网格单元?

python - 使用 tkinter 创建 .exe 文件,Vb 或 python 哪个更容易?

c# - 如何在代码中更改 GridView 中的图像 DataImageUrlFormatString 值?

c# - 如何转换引用打印字符串