private void EditProductsListBox_DrawItem(object sender, DrawItemEventArgs e) { ListBox listBox = (ListBox)sender; ProductModel listBoxItem = (ProductModel)listBox.Items[e.Index]; Color backgroundColor = listBoxItem.NeedsRefill ? Color.MistyRose : Color.LightGreen; ListBoxTools.DrawListBox(e, listBoxItem, backgroundColor, Brushes.Black); }
// Needs the property DrawMode set to OwnerDrawFixed in order to work. private void ProductsListBox_DrawItem(object sender, DrawItemEventArgs e) { ListBox listBox = (ListBox)sender; if (listBox.Items.Count < 1) { return; } // Avoids crash when ListBox is empty. ProductModel listBoxItem = (ProductModel)listBox.Items[e.Index]; Color backgroundColor = listBoxItem.NeedsRefill ? Color.MistyRose : Color.LightGreen; ListBoxTools.DrawListBox(e, listBoxItem, backgroundColor, Brushes.Black); }