I am facing a little issue in my project how can I change fore-color of text of selected items in ListBox. I can select all items of ListBox but I don't know how to change fore-color of text of selected items.
This code am using in my project for select listbox items
for (int i = 0; i < lbProductsToBuy.Items.Count; i++)
{
lbProductsToBuy.SetSelected(i,true);
}
printreceiptToken1();
dataGridView67.Rows.Clear();
Thanks. In these images you can see UI of my application. image1 and image2. See this last image, I want to change this selected items fore-color.
You can set DrawMode property of ListBox to OwnerDrawFixed and then hanlde DrawItem event of the control and draw items yourself:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
var listBox = sender as ListBox;
var backColor = this.BackColor; /*Default BackColor*/
var textColor = this.ForeColor; /*Default ForeColor*/
var txt = listBox.GetItemText(listBox.Items[e.Index]);
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backColor = Color.RoyalBlue; /*Seletion BackColor*/
textColor = Color.Yellow; /*Seletion ForeColor*/
}
using (var brush = new SolidBrush(backColor))
e.Graphics.FillRectangle(brush, e.Bounds);
TextRenderer.DrawText(e.Graphics, txt, listBox.Font, e.Bounds, textColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Left);
}

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With