system.windows.forms.listbox change item color
i couldnt manage to change system.windows.forms.listbox item color,
do you know how to do that?
thanks.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Nice post.
Set DrawMode property and handle a DrawItem event.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
what am i going to set the DrawMode to?
what is nice about the post?
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
what am i going to set the DrawMode to?
what is nice about the post?
Dude, You know everything.
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
no , i really dont know. there are three options to set for DrawMode, which one should i set? i debug the application and it does not hit DrawItem event handler.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
Here is a code.
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Items.Add("First");
listBox1.Items.Add("Second");
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.DrawItem += new DrawItemEventHandler(listBox1_DrawItem);
}
void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Red, e.Bounds);
}
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.Graphics.DrawString(
listBox1.Items[e.Index].ToString(),
e.Font,
Brushes.Red,
e.Bounds,
StringFormat.GenericDefault);
}
this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.listBox1.FormattingEnabled = true;
this.listBox1.Items.AddRange(new object[] {
"Item 1",
"Item 1",
"Item 1",
"Item 1"});
this.listBox1.Location = new System.Drawing.Point(90, 243);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(120, 95);
this.listBox1.TabIndex = 3;
this.listBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.listBox1_DrawItem);
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
Here is a code.
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Red, e.Bounds);
Nice choice of colors ;)
Its funny we both used red.
sknake
Industrious Poster
4,954 posts since Feb 2009
Reputation Points: 1,764
Solved Threads: 735
not only you but also i chose the red :D
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127
guys i try to encapsulate this functionality into a new inherited control named AdvancedListBox, but i wasnt able to do that. why dont you create a windows control library project for me and later we can add that as a toolbox item, instead of repeating code?
i would have done it if it was asp.net matter, but in windows forms i am not as good.
serkan sendur
Postaholic
2,062 posts since Jan 2008
Reputation Points: 854
Solved Threads: 127