Windows Develop Bookmark and Share   
 index > Windows Forms General > Customize Listbox Items
 

Customize Listbox Items

Hi everybody,

I have listbox object in my form. Now I want the first item I add to to be bold and the font color should be red. Then all the other items I add should be default. Is that possible ? If yes how ? If no, why not ?

Thanks for your help !!!
Thomas Schneider  Tuesday, December 27, 2005 3:50 PM
Sure! Use owner drawing! Set Listbox.DrawMode to OwnerDrawFixed or OwnerDrawVariable and paint the items yourself. Here's how.
Jelle van der Beek2  Wednesday, December 28, 2005 7:13 AM

This is all you need:

 

public class RedListBox : ListBox

{

public RedListBox()

{

DrawMode = DrawMode.OwnerDrawFixed;

}

protected override void OnDrawItem(DrawItemEventArgs e)

{

if (e.Index < 0 || e.Index >= Items.Count)

return;

Color itemForeColor = e.Index == 0 ?

Color.Red : e.ForeColor;

e.DrawBackground();

e.DrawFocusRectangle();

e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, new SolidBrush(itemForeColor), e.Bounds);

}

}

Eli Gazit  Wednesday, December 28, 2005 8:19 AM
Sure! Use owner drawing! Set Listbox.DrawMode to OwnerDrawFixed or OwnerDrawVariable and paint the items yourself. Here's how.
Jelle van der Beek2  Wednesday, December 28, 2005 7:13 AM

This is all you need:

 

public class RedListBox : ListBox

{

public RedListBox()

{

DrawMode = DrawMode.OwnerDrawFixed;

}

protected override void OnDrawItem(DrawItemEventArgs e)

{

if (e.Index < 0 || e.Index >= Items.Count)

return;

Color itemForeColor = e.Index == 0 ?

Color.Red : e.ForeColor;

e.DrawBackground();

e.DrawFocusRectangle();

e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, new SolidBrush(itemForeColor), e.Bounds);

}

}

Eli Gazit  Wednesday, December 28, 2005 8:19 AM

Hi Eli,

A tiny tweak on your code:

I try to make it standard practise to dispose of things like brushes. Like this:



using( Brush brush = new SolidBrush(itemForeColor) )
{
  e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, brush, e.Bounds);
}


 

Jelle van der Beek2  Wednesday, December 28, 2005 8:53 AM

You are right in general, best practive indeed, but when writing:

new Object() inside a method declartion, it is disposed right away, or, maybe I got it wrong?

 

 

Eli Gazit  Wednesday, December 28, 2005 8:56 AM

Actually... not necessarily. You are right that when a local object goes out of scope that there are no more references to it. But the garbage collector may take quite some time before disposing of the object. I've explained this in some other forum more extensively, along with a sample. You might want to check it out:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=172066&SiteID=1

Calling Dispose on an object doesn't mean that the object is freed. It is a way to free unmanaged resources, which we, forms programmers, have to take good care of. Windows Forms (actually GDI+) uses GDI handles extensively, and we should try to dispose of our objects as quickly as possible. If not done manually through Dispose or the using keyword, the object will surely be disposed later when the garbage collector kicks in. But that might be too late on intensive paint operations. GDI handles are limited. I make it standard practise to apply the using keyword to any object that implements the IDisposable interface.

Jelle van der Beek2  Wednesday, December 28, 2005 9:06 AM
Got it, Thanx man.
Eli Gazit  Wednesday, December 28, 2005 9:13 AM
Thank you guys !
Thomas Schneider  Wednesday, December 28, 2005 10:02 AM
Hi all,
working with this listbox, I have a problem if the size of item is bigger than size of listbox.
Have you any idea?
thanks in advances,
Sandrokan
Sandrokan  Monday, May 29, 2006 3:15 PM

You can use google to search for other answers

Custom Search

More Threads

• How to have a single icon in taskbar
• Using Access database in a webservice
• DataGrid Arbic Font ?????????????
• creating rows in listview control
• Calendar control help
• Smart client examples
• How to disable treeview node for good?
• Form.ShowDialog() closing when it SHOULDN'T be
• Mdi Parent with a navigation toolbar on it
• Need some information from all you smart follks... :)