i couldnt manage to change system.windows.forms.listbox item color,
do you know how to do that?
thanks.

Nice post.

Set DrawMode property and handle a DrawItem event.

what am i going to set the DrawMode to?
what is nice about the post?

what am i going to set the DrawMode to?
what is nice about the post?

Dude, You know everything.

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.

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);
                        
     }
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);

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.

not only you but also i chose the red :D

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.