此控件是MSDN和网络上的一些资料修改而来的。
控件用了ToolStripControlHost,是当下拉列表显示树的时候,主窗体不会事情焦点。
控件的树赋值加载节点和原来的一样。
有几个小程序要用到这个控件,都没时间去改。以前用的方法是:在树拖放一个节点到一个文本框中来处理。
先看控件的使用:控件下拉内容是一个树控件。
控件的源代码:
Imports System.Security.Permissions Namespace NF <SecurityPermissionAttribute(SecurityAction.LinkDemand,Flags:=SecurityPermissionFlag.UnmanagedCode)> _ Public Class ComboBoxTreeView Inherits ComboBox Private Const WM_USER As Integer = &H400 Private Const WM_REFLECT As Integer = WM_USER + &H1C00 Private Const WM_COMMAND As Integer = &H111 Private Const CBN_DROPDOWN As Integer = 7 Private Const WM_LBUTTONDOWN As Integer = &H201 Private Const WM_LBUTTONDBLCLK As Integer = &H203 Private treeViewHost As ToolStripControlHost Private Shadows dropDown As ToolStripDropDown Private _treeView As New TreeView Public Sub New() _treeView.BorderStyle = BorderStyle.None AddHandler _treeView.AfterSelect,AddressOf treeview_AfterSelect AddHandler _treeView.DoubleClick,AddressOf treeView_DoubleClick treeViewHost = New ToolStripControlHost(_treeView) dropDown = New ToolStripDropDown() dropDown.Width = Me.Width dropDown.Items.Add(treeViewHost) dropDown.AutoClose = True End Sub Public ReadOnly Property TreeView() As TreeView Get Return CType(treeViewHost.Control,TreeView) End Get End Property Public ReadOnly Property SelectedNode() As TreeNode Get Return TreeView.SelectedNode End Get End Property Private Sub ShowDropDown() If Not (dropDown Is Nothing) Then treeViewHost.Width = DropDownWidth treeViewHost.Height = DropDownHeight dropDown.Show(Me,Me.Height) End If End Sub Public Sub treeView_DoubleClick(sender As Object,e As EventArgs) dropDown.Close() End Sub Public Sub treeview_AfterSelect(sender As Object,e As TreeViewEventArgs) Me.Text = TreeView.SelectedNode.Text End Sub Public Shared Function HIWORD(ByVal n As Integer) As Integer Return (n >> 16) And &HFFFF End Function Protected Overrides Sub WndProc(ByRef m As Message) 'If m.Msg = WM_REFLECT + WM_COMMAND Then ' If HIWORD(CType(m.WParam,Integer)) = CBN_DROPDOWN Then ' ShowDropDown() ' Return ' End If 'End If 'MyBase.WndProc(m) If m.Msg = WM_LBUTTONDBLCLK OrElse m.Msg = WM_LBUTTONDOWN Then ShowDropDown() Return End If MyBase.WndProc(m) End Sub Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (dropDown Is Nothing) Then RemoveHandler TreeView.AfterSelect,AddressOf treeview_AfterSelect RemoveHandler TreeView.DoubleClick,AddressOf treeView_DoubleClick dropDown.Dispose() dropDown = Nothing End If End If MyBase.Dispose(disposing) End Sub End Class End Namespace
Public Class Form1 Dim root As New TreeNode Dim node As TreeNode Private Sub Form1_Load(sender As System.Object,e As System.EventArgs) Handles MyBase.Load root.Text = "根目錄" root.Name = 0 For i As Integer = 1 To 20 node = New TreeNode With {.Name = i,.Text = "node" & i,.Tag = i * 10} root.Nodes.Add(node) Next cboTreeView.TreeView.Nodes.Add(root) cboTreeView.TreeView.ExpandAll() End Sub Private Sub cboTreeView_TextChanged(sender As Object,e As System.EventArgs) Handles cboTreeView.TextChanged node = Me.cboTreeView.SelectedNode If node IsNot Nothing AndAlso node.Tag IsNot Nothing Then Me.TextBox1.Text = node.Tag.ToString Else Me.TextBox1.Text = "nothing" End If End Sub End Class
以前的相关内容:
VB.Net程序设计:在DataGridView附加多列显示CombBox控件的代码段。
http://www.jb51.cc/article/p-vlrrfked-ts.html