2016-12-31 4 views
1

Les éléments de la liste déroulante n'apparaissent pas. Ceci est le code que j'ai:Combobox Backcolor avec Dropdownlist est-il possible?

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) 

Et ce que je vois quand je l'exécute:

enter image description here

Qu'est-ce que je fais mal dans mon code?

Répondre

0

C'est la ligne qui fait que vous ne voyez pas tous les éléments:

ComboBox1.DrawMode = DrawMode.OwnerDrawFixed

C'est parce que vous avec cette ligne dire la Combobox: Hé, je fais moi-même dessin . Et à partir de ce moment-là, le Combobox déclenchera l'événement DrawItem quand il veut dessiner quelque chose et c'est à vous de vous y abonner et de le gérer. La gestion dans ce cas signifie: Dessiner quelque chose sur l'objet Graphics donné dans l'événement.

Voici une implémentation simple qui fait que:

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 

Si vous ne prévoyez pas de dessiner vos propres articles, vous pouvez retirer de la ligne bien sûr DrawMode ...

Voici le résultat avec mon code:

owner drawn combobox