实现 ComboBox下拉显示表格

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了实现 ComboBox下拉显示表格相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
实现效果如下:

当点击combobox箭头后显示一个Datagridview表格。
一个网友提出来的问题,根据网上的代码修改。
首先实现comboDataGridView类,具体代码如下:

Public Class comboDataGridView
    Inherits ComboBox

    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONDBLCLK = &H203

    Dim tscHost As ToolStripControlHost
    Dim tsdropDown As ToolStripDropDown

    Dim myDgv As DataGridView

    '要在combobox中显示文本对应的列
    Public Property showTextColumnId As Integer

    '要在dgv中显示的数据
    Public dt As DataTable

    Public Sub New(ByVal width As Integer, ByVal dt As DataTable, ByVal parent As Form)
        myDgv = New DataGridView

        myDgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        myDgv.RowHeadersVisible = False
        myDgv.MultiSelect = False
        myDgv.AllowUserToAddRows = False
        myDgv.AllowUserToDeleteRows = False
        myDgv.AllowUserToResizeRows = False
        myDgv.ReadOnly = True

        parent.Controls.Add(myDgv)


        myDgv.DataSource = dt

        AddHandler myDgv.MouseDown, AddressOf dgv_MouseDown

        showTextColumnId = -1

        tscHost = New ToolStripControlHost(myDgv)
        tscHost.AutoSize = False

        tsdropDown = New ToolStripDropDown
        tsdropDown.Width = width
        tsdropDown.Items.Clear()
        tsdropDown.Items.Add(tscHost)

        myDgv.Visible = False
        Me.dt = dt
    End Sub

    Public Sub addData()
        myDgv.DataSource = dt
        myDgv.Refresh()
    End Sub

    'dgv按下鼠标键
    Private Sub dgv_MouseDown(sender As Object, e As MouseEventArgs)

        Dim dgv As DataGridView = CType(sender, DataGridView)

        Dim dgvhti As DataGridView.HitTestInfo = dgv.HitTest(e.X, e.Y)
        Dim selectedIndex As Integer
        If dgvhti.Type = DataGridViewHitTestType.Cell Or dgvhti.Type = DataGridViewHitTestType.RowHeader Then
            If showTextColumnId < 0 Then
                Exit Sub
            End If

            selectedIndex = dgvhti.RowIndex
            Dim textToShow As String
            textToShow = dgv.Rows(selectedIndex).Cells(showTextColumnId).Value.ToString
            Me.Text = textToShow
        End If
    End Sub


    Private Sub ShowDropDown()
        myDgv.Visible = True
        If IsNothing(tsdropDown) = False Then
            tscHost.Size = New Size(300, 200)   'Me.Width - 2, dropDown.Height)
            tsdropDown.Show(Me, 0, Me.Height)

            myDgv.Rows(0).Selected = True
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        If (m.Msg = WM_LBUTTONDBLCLK) Or (m.Msg = WM_LBUTTONDOWN) Then
            Call ShowDropDown()
        End If
        MyBase.WndProc(m)
    End Sub

    Protected Overrides Sub Dispose(disposing As Boolean)
        If disposing = True Then
            If IsNothing(tsdropDown) = False Then
                tsdropDown.Dispose()
                tsdropDown = Nothing
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
End Class

在窗体中的使用:

    Dim cdgvt As comboDataGridView
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim dt As DataTable = getdt("学生")


        cdgvt = New comboDataGridView(200, dt, Me)
        cdgvt.Top = 60
        cdgvt.Left = 40

        cdgvt.DropDownHeight = 1

        cdgvt.showTextColumnId = 1

        Me.Controls.Add(cdgvt)
        cdgvt.Visible = True

        cdgvt.addData()
    End Sub

    Private Function getdt(ByVal tablename As String) As DataTable
        Dim xm() As String = {"赵柯", "钱廷军", "张强", "王睿", "李军", "周国轩", "郑蕾", "王海"}
        Dim xb() As String = {"男", "男", "男", "男", "男", "女", "女", "男"}
        Dim dt As New DataTable(tablename)
        dt.Columns.Add("学号")
        dt.Columns.Add("姓名")
        dt.Columns.Add("性别")

        For i As Integer = 0 To xm.Length - 1
            dt.Rows.Add(New String() {i, xm(i), xb(i)})
        Next

        Return dt
    End Function

目前存在的问题,第一下点击datagridview中的某行,dgv并不会接收到鼠标单击或者双击,第二下才可以。

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于实现 ComboBox下拉显示表格的主要内容,如果未能解决你的问题,请参考以下文章

沫沫金miniui表格DataGrid动态Combobox功能实现

VB combobox怎么实现可多选下拉列表中的内容?

C# 模糊查询带出下拉列表的问题

新手:delphi中combobox下拉框问题

delphi 如何让Combobox下拉菜单时,显示一个树形结构

如何令comboBox不能输入,只能选择