自定义控件之节点为下拉列表框的树型控件

效果图如下:



代码如下:

Public   Class DropDownTreeView
    
Inherits TreeView

    
Private WithEvents m_CurrentNode As DropDownTreeNode = Nothing

    
Protected Overrides Sub OnNodeMouseClick(ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs)
        
If e.Node.GetType.ToString.Equals("DropDownTreeNode"Then
            
Me.m_CurrentNode = CType(e.Node, DropDownTreeNode)
            
Me.Controls.Add(Me.m_CurrentNode.ComboBox)
            
Me.m_CurrentNode.ComboBox.SetBounds(Me.m_CurrentNode.Bounds.X - 1, _
                                                
Me.m_CurrentNode.Bounds.Y - 2, _
                                                
Me.m_CurrentNode.Bounds.Width + 25, _
                                                
Me.m_CurrentNode.Bounds.Height)

            
AddHandler Me.m_CurrentNode.ComboBox.SelectedValueChanged, AddressOf Me.ComboBox_SelectedValueChanged
            
AddHandler Me.m_CurrentNode.ComboBox.DropDownClosed, AddressOf Me.ComboBox_DropDownClosed

            
Me.m_CurrentNode.ComboBox.Show()
            
Me.m_CurrentNode.ComboBox.DroppedDown = True
        
End If
        
MyBase.OnNodeMouseClick(e)
    
End Sub


    
Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        HideComboBox()
        
MyBase.OnMouseWheel(e)
    
End Sub


    
Private Sub ComboBox_SelectedValueChanged(ByVal sender As ObjectByVal e As System.EventArgs)
        HideComboBox()
    
End Sub


    
Private Sub ComboBox_DropDownClosed(ByVal sender As ObjectByVal e As System.EventArgs)
        HideComboBox()
    
End Sub


    
Private Sub HideComboBox()
        
If Me.m_CurrentNode IsNot Nothing Then
            
RemoveHandler Me.m_CurrentNode.ComboBox.DropDownStyleChanged, AddressOf Me.ComboBox_DropDownClosed
            
RemoveHandler Me.m_CurrentNode.ComboBox.SelectedValueChanged, AddressOf Me.ComboBox_SelectedValueChanged

            
Me.m_CurrentNode.Text = Me.m_CurrentNode.ComboBox.Text
            
Me.m_CurrentNode.ComboBox.Hide()
            
Me.m_CurrentNode.ComboBox.DroppedDown = False
            
Me.Controls.Remove(Me.m_CurrentNode.ComboBox)
            
Me.m_CurrentNode = Nothing
        
End If
    
End Sub


End Class



Public   Class DropDownTreeNode
    
Inherits TreeNode

    
Private m_ComboBox As ComboBox = New ComboBox

    
Public Sub New()
        
MyBase.New()
    
End Sub


    
Public Sub New(ByVal text As String)
        
MyBase.New(text)
    
End Sub


    
Public Sub New(ByVal text As StringByVal children As TreeNode())
        
MyBase.New(text, children)
    
End Sub


    
Public Property ComboBox() As ComboBox
        
Get
            m_ComboBox.DropDownStyle 
= ComboBoxStyle.DropDownList
            
Return m_ComboBox
        
End Get
        
Set(ByVal value As ComboBox)
            m_ComboBox 
= value
            m_ComboBox.DropDownStyle 
= ComboBoxStyle.DropDownList
        
End Set
    
End Property


End Class

你可能感兴趣的:(自定义控件之节点为下拉列表框的树型控件)