blah blah blah is here! blah blah » Close

up0down
link

Howdy, Please help me.

I wonder how to give a different color for each listbox item. I've try to make a short line of code to handle that but it was failed.

if(this.listBox1.InvokeRequired==true){
this.listBox1.Invoke((MethodInvoker)delegate(){
if(type=="Error"){
listBox1.ForeColor = System.Drawing.Color.Red;
}else if(type=="Info"){
listBox1.ForeColor = System.Drawing.Color.Blue;
}else if(type=="Success"){
listBox1.ForeColor = System.Drawing.Color.Green;
}
listBox1.Items.Add(type+": " + arg);
});
}else{
if(type=="error"){
listBox1.ForeColor = System.Drawing.Color.Red;
}else if(type=="info"){
listBox1.ForeColor = System.Drawing.Color.Blue;
}else if(type=="success"){
listBox1.ForeColor = System.Drawing.Color.Green;
}
listBox1.Items.Add(type+": " + arg);
}

last answered one year ago

1 answers

up0down
link

To do something like this, you have to draw the listbox items yourself by handling the DrawItem event.

There's some sample code here.

If you add the eventhandler skeleton by double clicking on the DrawItem event in the Properties Box, then you won't need to carry out the final step of wiring up the eventhandler - the VS designer will do it automatically for you.

Feedback