Specifying the color for a listbox item from the drawItemEventHandler
Here's the code I have to specify the color of a particular row of a ListBox:
private void listBox1_DrawItem(object sender, DrawItemEventArgs e, Color color) {
e.DrawBackground();
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
g.DrawString(Text, e.Font, new SolidBrush(color), e.Bou开发者_如何学JAVAnds);
e.DrawFocusRectangle();
}
I want to be able to pass color to this method, but don't quite know how to tell the method which color to use when it's called. How can I accomplish this?
Simply use a private field of your class. Or if the color should be based on which particular item is being drawn, the typical case, then use e.Index to know which item is being drawn. Watch out for -1.
I hope I have not misunderstood your question but isn't the background and foreground color available in the drawitemEventArgs parameter already?
If so, there would not be a need to add a 3rd parameter (color).
Also, this would not be a good practice in my opinion because you will notice (by looking at other event handlers throughout your application) that .net standardizes all events as having 2 parameters a "sender" and an "e" parameter which can be of any type.
But double check that drawItemEventArgs parameter. I believe the color is there and can be set.
thanks
精彩评论