Treeview节点有更多信息

时间:2018-03-27 16:13:58

标签: .net vb.net winforms treeviewitem

我正在尝试在树视图节点中自定义信息,所以我创建了这个类

Public Class TreeViewItem
    Inherits TreeNode
    Private _text As String
    Private _id As String
    Private _Data As String

    Sub New(id As String, name As String, data As String)
        MyBase.New()
        _text = Name
        _id = id
        _Data = data

    End Sub

    Public Shadows Property Text As String

        Get
            If Not String.IsNullOrEmpty(_Data) Then
                Return String.Format("{0} -> {1}", _text, _Data)
            Else
                Return _text
            End If
        End Get
        Set(value As String)
            _text = Name
        End Set
    End Property
    Public Property ID As String
        Get
            Return _id
        End Get
        Set(value As String)
            _id = value
        End Set
    End Property

    Public Property Data As String
        Get
            Return _Data
        End Get
        Set(value As String)
            _Data = value
        End Set
    End Property

End Class

然而,当我添加像这样的节点

tv.Nodes.Add(New TreeViewItem(1, "hello", "hi"))

节点的文本是空的,为什么它没有呈现的任何帮助?

1 个答案:

答案 0 :(得分:2)

您几乎必须使用基类的Text属性,因此请尝试使用重载,以便设置值:

Public Overloads Property Text As String
  Get
    Return MyBase.Text
  End Get
  Set(value As String)
    _text = value
    If Not String.IsNullOrEmpty(_Data) Then
      MyBase.Text = String.Format("{0} -> {1}", value, _Data)
    Else
      MyBase.Text = value
    End If
  End Set
End Property

这会将您的构造函数更改为:

Sub New(id As String, name As String, data As String)
  MyBase.New()
  _id = id
  _Data = data
  _text = name
  Me.Text = _text
End Sub

您的数据属性的更新:

Public Property Data As String
  Get
    Return _Data
  End Get
  Set(value As String)
    _Data = value
    Me.Text = _text
  End Set
End Property