I know that this code
public void FillListBoxColoredText(ListBox LBx, List liststX)
{
int iCount = liststX.Count();
for (int ii = 0; ii < iCount; ii++) {
if (liststX[ii].Contains("blue"))
LBx.Items.Add(liststX[ii], Foreground = #0000ff);
else if (liststX[ii].Contains("red"))
LBx.Items.Add(liststX[ii], Foreground = #ff0000);
else if (liststX[ii].Contains("green"))
LBx.Items.Add(liststX[ii], Foreground = #00ff00);
}
is not correct. It strives to indicate what I want.
Jayraj ChhayaPosted Mar 20, 2024, 7:45 AM
Try this code
Sarthak VarshneyPosted Mar 20, 2024, 4:40 AM
To change the foreground color of items in a ListBox programmatically in C#, you can use the DrawItem event and customize the drawing of each item.
the
listBox1_DrawItemevent handler is used to customize the drawing of each item in theListBox. It checks the text of each item and sets the colour accordingly. You'll need to adjust the colour logic to suit your specific requirements.