【问题标题】:Multi column combobox change width多列组合框更改宽度
【发布时间】:2016-01-07 00:21:35
【问题描述】:

我有一个包含三列的 vb.net 组合框。 如何使 e.bounds 更宽?我想设置组合框的下拉菜单 比实际的组合框更宽。

我使用了以下代码:

   Private Sub BlokKiesCMB_DrawItem(ByVal sender As 
   System.Object, ByVal e As
   System.Windows.Forms.DrawItemEventArgs) 
   Handles BlokKiesCMB.DrawItem
    ' Draw the default background
    e.DrawBackground()

    ' The ComboBox is bound to a DataTable,
    ' so the items are DataRowView objects.
    Dim drv As DataRowView = CType(BlokKiesCMB.Items(e.Index), DataRowView)


    ' Retrieve the value of each column.
    Dim blokno As String = drv("blokno").ToString()
    Dim kultivar As String = drv("kultivar").ToString()
    Dim klas As String = drv("klas").ToString()

    ' Get the bounds for the first column

    Dim r1 As Rectangle = e.Bounds
    r1.Width = r1.Width / 3

    ' Draw the text on the first column
    Using sb As SolidBrush = New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(blokno, e.Font, sb, r1)
    End Using

    ' Draw a line to isolate the columns 
    Using p As Pen = New Pen(Color.Black)
        e.Graphics.DrawLine(p, r1.Right, 0, r1.Right, r1.Bottom)
    End Using

    ' Get the bounds for the second column
    Dim r2 As Rectangle = e.Bounds
    r2.X = e.Bounds.Width / 3
    r2.Width = r2.Width / 3

    ' Draw the text on the second column
    Using sb As SolidBrush = New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(kultivar, e.Font, sb, r2)
    End Using

    ' Draw a line to isolate the columns 
    Using p As Pen = New Pen(Color.Black)
        e.Graphics.DrawLine(p, r2.Right, 0, r2.Right, r2.Bottom)
    End Using



    ' Get the bounds for the third column
    Dim r3 As Rectangle = e.Bounds
    r3.X = r2.Width + r2.X
    r3.Width = r3.Width / 3



    ' Draw the text on the third column
    Using sb As SolidBrush = New SolidBrush(e.ForeColor)
        e.Graphics.DrawString(klas, e.Font, sb, r3)
    End Using


End Sub

【问题讨论】:

    标签: vb.net combobox


    【解决方案1】:

    e.Bounds 不会为您提供ComboBox 或其DropDown 框的侧面。 Bounds 指的是 ComboBox 相对于其父对象的位置。阅读this 以获取更多参考。

    现在,WinFormsComboBox 有一个名为DropDownWidth 的属性,它允许您控制DropDown 的宽度比ComboBox 的实际宽度更宽。此属性默认设置为与ComboBox 的宽度相同的值。

    这里是实际示例,ComboBox 的大小为 121,默认情况下其DropDown 框的大小也是 121。

    但是如果你把DropDownWidth(比如,改为200)改成200

    Me.ComboBox1.DropDownWidth = 200 'change this
    

    这就是你要得到的东西

    ComboBox 下拉框变宽了。

    【讨论】:

    • 嗨伊恩。效果很好。非常感谢。
    • 谢谢伊恩。我将来会这样做。问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-25
    • 2022-01-13
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多