2016-01-07 5 views
1

У меня есть vb.net combobox с тремя столбцами. Как сделать e.bounds более широким? Я хочу установить раскрывающийся список combobox шире, чем фактическое поле со списком.Multi column combobx change width

Я использовал следующий код:

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 

С уважением и спасибо

ответ

1

e.Bounds не даю вам сторону ComboBox или его DropDown поля. Bounds относится к положению ComboBox относительно его родителя. Прочтите this для получения дополнительной информации.

Теперь WinFormsComboBox имеет свойство DropDownWidth, которая позволяет контролировать ширину DropDown шире, чем ширина фактических ComboBox «ы. Это свойство по умолчанию устанавливается как то же значение, что и ширина ComboBox.

Настоящий пример: размер ComboBox равен 121, по умолчанию размер его DropDown будет равен 121.

enter image description here

Но если вы измените DropDownWidth (говорит 200)

Me.ComboBox1.DropDownWidth = 200 'change this 

Это то, что вы собираетесь получить

enter image description here

В ComboBox выпадающий стал шире.

+0

Hi Ian. Прекрасно работает. Большое спасибо. – Gideon

+0

Спасибо Яну. Я сделаю это в будущем. С уважением – Gideon