2014-05-13 8 views
0

Je me demandais s'il était possible de créer une liste déroulante (liste déroulante) et de peindre des cellules spécifiques dans la liste déroulante. donc, si j'ai cinq éléments dans la liste déroulante, le deuxième élément et le dernier élément seront peints en bleu par exemple et les autres en gris .. est-ce possible?liste déroulante - liste déroulante

System.Windows.Forms.ComboBox comboBox; 
comboBox = new System.Windows.Forms.ComboBox(); 
comboBox.AllowDrop = true; 
comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
comboBox.FormattingEnabled = true; 
comboBox.Items.AddRange(excel.getPlanNames()); 
comboBox.Location = new System.Drawing.Point(x, y); 
comboBox.Name = "comboBox1"; 
comboBox.Size = new System.Drawing.Size(sizeX, sizeY); 
comboBox.TabIndex = 0; 
group.Controls.Add(comboBox); 

remercient pour toute aide ..

+1

[Microsoft fournit un exemple dans l'aide MSDN] (http://msdn.microsoft.com/fr-fr/library/system.windows.forms.combobox.drawitem%28v=vs.110%29.aspx) –

Répondre

2

Vous pouvez utiliser l'événement DrawItem.

D'abord, vous devez définir le DrawMode du ComboBox à OwnerDrawFixed

Ensuite, vous définissez la DrawItem à quelque chose comme ce qui suit:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) 
{ 
    Font font = (sender as ComboBox).Font; 
    Brush backgroundColor; 
    Brush textColor; 

    if (e.Index == 1 || e.Index == 3) 
    { 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     { 
      backgroundColor = Brushes.Red; 
      textColor = Brushes.Black; 
     } 
     else 
     { 
      backgroundColor = Brushes.Green; 
      textColor = Brushes.Black; 
     } 
    } 
    else 
    { 
     if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) 
     { 
      backgroundColor = SystemBrushes.Highlight; 
      textColor = SystemBrushes.HighlightText; 
     } 
     else 
     { 
      backgroundColor = SystemBrushes.Window; 
      textColor = SystemBrushes.WindowText; 
     } 
    } 
    e.Graphics.FillRectangle(backgroundColor, e.Bounds); 
    e.Graphics.DrawString((sender as ComboBox).Items[e.Index].ToString(), font, textColor, e.Bounds); 
} 

Cet exemple fera la couleur de fond par défaut vert avec du texte noir , et l'élément en surbrillance aura un fond rouge et un texte noir, des éléments aux index 1 et 3.

Vous pouvez également définir la police d'ind éléments individuels utilisant la variable font.

+1

m'a sauvé .. merci beaucoup :))) –