继承创建自定义的TreeView
CustomTreeViewForm类,好东西大家分享,好好研究一下哦!
Public Class CustomTreeViewForm
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
InitializeForm()
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents tvwCategories As System.Windows.Forms.TreeView
Friend WithEvents txtSelectedID As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.tvwCategories = New System.Windows.Forms.TreeView()
Me.txtSelectedID = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'tvwCategories
'
Me.tvwCategories.ImageIndex = -1
Me.tvwCategories.Location = New System.Drawing.Point(7, 2)
Me.tvwCategories.Name = "tvwCategories"
Me.tvwCategories.SelectedImageIndex = -1
Me.tvwCategories.Size = New System.Drawing.Size(277, 240)
Me.tvwCategories.TabIndex = 0
'
'txtSelectedID
'
Me.txtSelectedID.Location = New System.Drawing.Point(6, 246)
Me.txtSelectedID.Name = "txtSelectedID"
Me.txtSelectedID.Size = New System.Drawing.Size(278, 20)
Me.txtSelectedID.TabIndex = 1
Me.txtSelectedID.Text = "txtSelectedID"
'
'CustomTreeViewForm
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtSelectedID, Me.tvwCategories})
Me.Name = "CustomTreeViewForm"
Me.Text = "Custom TreeView"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub InitializeForm()
Dim objCategories As New Categories()
Dim i As Integer
Dim x As Integer
For i = 1 To 5
With objCategories.Add(New Category())
.CategoryID = "CAT" & i
.DisplayName = "Category " & i
For x = 1 To 3
With .AccountTypes.Add(New AccountType())
.AccountTypeID = "A" & x
.DisplayName = "AccountType " & x
End With
With .PartTypes.Add(New PartType())
.PartTypeID = "P" & x
.DisplayName = "PartType " & x
End With
Next x
End With
Next i
With Me.tvwCategories
Dim objCategory As Category
Dim objAccountType As AccountType
Dim objPartType As PartType
Dim objCategoryTreeNode As CategoryTreeNode
For Each objCategory In objCategories
objCategoryTreeNode = New CategoryTreeNode(objCategory)
.Nodes.Add(objCategoryTreeNode)
With objCategoryTreeNode
For Each objAccountType In objCategory.AccountTypes
.Nodes.Add(New AccountTypeTreeNode(objAccountType))
Next
For Each objPartType In objCategory.PartTypes
.Nodes.Add(New PartTypeTreeNode(objPartType))
Next
End With
Next
End With
End Sub
Private Sub tvwCategories_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwCategories.AfterSelect
Dim objNode As TreeNode
objNode = tvwCategories.SelectedNode
Select Case objNode.GetType.ToString
Case "CustomTreeView.CategoryTreeNode"
Dim objCategoryTreeNode As CategoryTreeNode
objCategoryTreeNode = CType(objNode, CategoryTreeNode)
txtSelectedID.Text = objCategoryTreeNode.Category.CategoryID.ToString
Case "CustomTreeView.AccountTypeTreeNode"
Dim objAccountTypeTreeNode As AccountTypeTreeNode
objAccountTypeTreeNode = CType(objNode, AccountTypeTreeNode)
txtSelectedID.Text = objAccountTypeTreeNode.AccountType.AccountTypeID.ToString
Case "CustomTreeView.PartTypeTreeNode"
Dim objPartTypeTreeNode As PartTypeTreeNode
objPartTypeTreeNode = CType(objNode, PartTypeTreeNode)
txtSelectedID.Text = objPartTypeTreeNode.PartType.PartTypeID.ToString
End Select
End Sub
End Class
Public Class AccountType
Public AccountTypeID As String
Public DisplayName As String
End Class
Public Class PartType
Public PartTypeID As String
Public DisplayName As String
End Class
Public Class Category
Public CategoryID As String
Public DisplayName As String
Public AccountTypes As New AccountTypes()
Public PartTypes As New PartTypes()
End Class
Public Class Categories
Inherits CollectionBase
Public Function Add(ByVal value As Category) As Category
Me.InnerList.Add(value)
Add = value
End Function
End Class
Public Class AccountTypes
Inherits CollectionBase
Public Function Add(ByVal value As AccountType) As AccountType
Me.InnerList.Add(value)
Add = value
End Function
End Class
Public Class PartTypes
Inherits CollectionBase
Public Function Add(ByVal value As PartType) As PartType
Me.InnerList.Add(value)
Add = value
End Function
End Class
Public Class AccountTypeTreeNode
Inherits TreeNode
Public AccountType As AccountType
Sub New(ByVal Value As AccountType)
MyBase.New()
AccountType = Value
Me.Text = AccountType.DisplayName
End Sub
End Class
Public Class PartTypeTreeNode
Inherits TreeNode
Public PartType As PartType
Sub New(ByVal Value As PartType)
MyBase.New()
PartType = Value
Me.Text = PartType.DisplayName
End Sub
End Class
Public Class CategoryTreeNode
Inherits TreeNode
Public Category As Category
Sub New(ByVal Value As Category)
MyBase.New()
Category = Value
Me.Text = Category.DisplayName
End Sub
End Class