Comment définir la couleur d'arrière-plan d'un élément spécifique dans un System.Windows.Forms.ListBox? J'aimerais pouvoir en mettre plusieurs si possible.Couleur d'arrière-plan d'un élément ListBox (winforms)
Répondre
Probablement la seule façon d'accomplir cela est de dessiner les éléments vous-même.
Réglez le DrawMode
à OwnerDrawFixed
et le code quelque chose comme ceci sur l'événement DrawItem:
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
// Print text
e.DrawFocusRectangle();
}
Deuxième option serait d'utiliser un ListView, bien qu'ils aient un autre moyen de mises en œuvre (pas vraiment données lié, mais plus flexible à la façon des colonnes)
// Set the background to a predefined colour
MyListBox.BackColor = Color.Red;
// OR: Set parts of a color.
MyListBox.BackColor.R = 255;
MyListBox.BackColor.G = 0;
MyListBox.BackColor.B = 0;
Si vous voulez définir plusieurs backgroun d couleurs est prêt à établir une couleur de fond différente pour chaque élément, ce n'est pas possible avec un ListBox, mais il est un ListView, avec quelque chose comme:
// Set the background of the first item in the list
MyListView.Items[0].BackColor = Color.Red;
Merci pour le answer by Grad van Horck, il m'a guidé dans la bonne direction .
Pour soutenir le texte (pas seulement la couleur de fond) voici mon code entièrement travail:
//global brushes with ordinary/selected colors
private SolidBrush reportsForegroundBrushSelected = new SolidBrush(Color.White);
private SolidBrush reportsForegroundBrush = new SolidBrush(Color.Black);
private SolidBrush reportsBackgroundBrushSelected = new SolidBrush(Color.FromKnownColor(KnownColor.Highlight));
private SolidBrush reportsBackgroundBrush1 = new SolidBrush(Color.White);
private SolidBrush reportsBackgroundBrush2 = new SolidBrush(Color.Gray);
//custom method to draw the items, don't forget to set DrawMode of the ListBox to OwnerDrawFixed
private void lbReports_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
bool selected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
int index = e.Index;
if (index >= 0 && index < lbReports.Items.Count)
{
string text = lbReports.Items[index].ToString();
Graphics g = e.Graphics;
//background:
SolidBrush backgroundBrush;
if (selected)
backgroundBrush = reportsBackgroundBrushSelected;
else if ((index % 2) == 0)
backgroundBrush = reportsBackgroundBrush1;
else
backgroundBrush = reportsBackgroundBrush2;
g.FillRectangle(backgroundBrush, e.Bounds);
//text:
SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location);
}
e.DrawFocusRectangle();
}
adds ci-dessus au code donné et affiche le texte proprement dit, plus fort élément sélectionné.
Excellent, le bit sélectionné était très utile. – Almo
Qu'est-ce que reportsForegroundBrushSelected: reportsForegroundBrush ?? –
reportsForegroundBrushSelected: reportsForegroundBrush me donne une erreur, ils sont supposés être déclarés mais comment? –
public Picker()
{
InitializeComponent();
this.listBox.DrawMode = DrawMode.OwnerDrawVariable;
this.listBox.MeasureItem += listBoxMetals_MeasureItem;
this.listBox.DrawItem += listBoxMetals_DrawItem;
}
void listBoxMetals_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
var item = listBox.Items[e.Index] as Mapping;
if (e.Index % 2 == 0)
{
e.Graphics.FillRectangle(new SolidBrush(Color.GhostWhite), e.Bounds);
}
e.Graphics.DrawString(item.Name,
e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
échantillon complet
private void listbox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
e.DrawBackground();
Brush myBrush = Brushes.Black;
var item = listbox1.Items[e.Index];
if(e.Index % 2 == 0)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Gold), e.Bounds);
}
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(),
e.Font, myBrush,e.Bounds,StringFormat.GenericDefault);
e.DrawFocusRectangle();
}
public MainForm()
{
InitializeComponent();
this.listbox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listbox1_DrawItem);
}
- 1. WPF ListBox Selection Couleur
- 2. Silverlight Listbox Élément Style
- 3. WinForms: couleur Bordure DataGridView
- 4. Listbox élément problème de premier plan
- 5. WPF ListBox Button Élément sélectionné
- 6. valeurs sélectionnées de la listbox (winforms)
- 7. WPF affichage Listbox élément suivant après SelectedItem
- 8. ListBox sélectionne toujours automatiquement le premier élément
- 9. Définition de la couleur d'arrière-plan de Silverlight Listbox
- 10. Comment lier le dictionnaire à ListBox dans WinForms
- 11. Remplissez la couleur dans la zone de liste Cercle dans C# .Net dans Winforms
- 12. Comment convertir un indice numérique Tcl Listbox en son élément
- 13. Comment obtenir le dernier élément sélectionné dans ListBox multisélect?
- 14. Comment définir l'index ZI d'un élément Listbox sur MouseEnter?
- 15. Obtenir un élément WPF ListBox à partir de MouseLeftButtonDown
- 16. TextBox, Button et ListBox dans un ListBox
- 17. listbox scrolling
- 18. ListBox Objet ciblé
- 19. Ajouter un rectangle à WPF Listbox et faire alterner la couleur de remplissage de ce rectangle
- 20. Silverlight - Arrêter les hautes lumières sur une listbox
- 21. WPF ListBox databinding
- 22. Double buffered ListBox
- 23. Supprimer l'élément de ListBox?
- 24. ListBox éléments longs conseils
- 25. WPF listbox empty datatemplate
- 26. WinForms TreeView
- 27. Comment afficher et stocker une liste d'éléments complexes à l'aide de Winforms?
- 28. Listbox ClearSelected laisse les éléments sélectionnés
- 29. WPF ListBox + Binding + IDataErrorInfo =?
- 30. Définition de l'arrière-plan de WinForm ListBox
Il est possible avec un ListBox. Voir http://stackoverflow.com/questions/91747/background-color-of-a-listbox-item-winforms#91758 – jfs
s/possible/facile /. Tant pis. C# 1, novice 0. Je n'avais pas beaucoup travaillé avec des méthodes de peinture surchargées auparavant. –
le BackColor n'est pas une propriété de 'ListBox.ObjectCollection' élément – ghiboz