Combobox Backcolor可以下载列表吗?

时间:2016-12-31 18:12:39

标签: vb.net winforms drop-down-menu combobox backcolor

组合框中的项目不会出现。这是我的代码:

    ComboBox1.BackColor = Color.White
    ComboBox1.ForeColor = Color.Black
    ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
    ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
    ComboBox1.FlatStyle = FlatStyle.Standard
    ComboBox1.Items.Add("LIne 1")
    ComboBox1.Items.Add("LIne 2")
    ComboBox1.Items.Add("LIne 3")
    ComboBox1.Items.Add("LIne 4")
    ComboBox1.Items.Add("LIne 5")
    ComboBox1.Items.Add("LIne 6")
    ComboBox1.Text = ComboBox1.Items(0)

这就是我执行时所看到的:

enter image description here

我的代码中出错了什么?

1 个答案:

答案 0 :(得分:0)

这条线使您看不到任何物品:

ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

那是因为你用那条线告诉Combobox:嘿,我自己做绘图。从那一刻开始,当它想要绘制某些内容时,Combobox会引发事件DrawItem,由你来订阅并处理它。在这种情况下处理意味着:在事件中的给定Graphics对象上绘制一些东西。

这是一个简单的实现:

Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
    Dim Brush = Brushes.Black
    If (e.State And DrawItemState.ComboBoxEdit) = DrawItemState.ComboBoxEdit Then
        Brush = Brushes.Yellow
    End If
    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
        Brush = Brushes.SteelBlue
    End If

    Dim Point = New Point(2, e.Index * e.Bounds.Height + 1)

    ' reset 
    e.Graphics.FillRectangle(New SolidBrush(ComboBox1.BackColor),
        New Rectangle(
            Point,
            e.Bounds.Size))
    ' draw content
    e.Graphics.DrawString(
        ComboBox1.Items(e.Index).ToString(),
        ComboBox1.Font,
        Brush,
        Point)
End Sub

如果您不打算自己绘制项目,可以选择删除DrawMode线...

以下是我的代码的结果:

owner drawn combobox