VB.Net程序设计:下拉列表树控件ComboBoxTreeView

    此控件是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, 0, 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://blog.csdn.net/linjimu/article/details/5070936

VB.Net自己写的一个控件:ComboBox下拉列表中显示多列数据(可以绑定数据表)

http://blog.csdn.net/linjimu/article/details/1861641

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 DataGridView 控件的 EditMode 属性设置为 EditOnEnter 或 EditOnKeystrokeOrF2 时,可以在单元格内生成下拉列表控件。 以下是在单元格内生成下拉列表控件的示例代码: 1. 在 Form_Load 事件中添加以下代码,创建一个 DataTable 并将其绑定到 DataGridView 控件: ``` Dim dt As New DataTable dt.Columns.Add("Name") dt.Columns.Add("Gender") dt.Columns.Add("Age") dt.Rows.Add("John", "Male", "25") dt.Rows.Add("Mary", "Female", "30") dt.Rows.Add("Tom", "Male", "35") DataGridView1.DataSource = dt ``` 2. 在 DataGridView1_EditingControlShowing 事件中添加以下代码,判断当前单元格是否是“Gender”列,如果是,则创建一个 ComboBox 控件并将其绑定到单元格: ``` Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing If DataGridView1.CurrentCell.ColumnIndex = 1 Then '判断当前单元格是否是“Gender”列 Dim comboBox As ComboBox = TryCast(e.Control, ComboBox) If comboBox IsNot Nothing Then comboBox.DropDownStyle = ComboBoxStyle.DropDownList '设置 ComboBox 控件下拉列表样式为 DropDownList comboBox.Items.Clear() comboBox.Items.Add("Male") comboBox.Items.Add("Female") End If End If End Sub ``` 注意:在 DataGridView1_EditingControlShowing 事件中创建 ComboBox 控件时,要将其下拉列表样式设置为 DropDownList,这样用户就只能从下拉列表中选择一个值。 完成上述操作后,在单元格内单击并进入编辑模式时,就会出现一个下拉列表控件。用户可以从下拉列表中选择一个值,然后按 Enter 键或单击其他单元格来保存所选值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值