繼承建立自訂的TreeView

來源:互聯網
上載者:User
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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。