2016-12-31 19 views
1

組合框中的項目不出現。這是我的代碼有:組合框Backcolor和Dropdownlist是可能的嗎?

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

我在做什麼錯在我的代碼?

回答

0

這是使你看不到任何物品行:

ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

這是因爲與該行你告訴組合框:嘿,我做的是我自己畫 。從那一刻起,Combobox將在事件上提起事件,並由您來訂閱並處理。在這種情況下的處理意味着:在事件中給定的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

相關問題