Click here to Skip to main content
15,880,608 members
Articles / Programming Languages / C#
Article

An auto-resize C# Listbox (updated)

Rate me:
Please Sign up or sign in to vote.
3.74/5 (13 votes)
2 Aug 2004 178.1K   4.7K   64   20
A listbox which will auto-resize its texts.

Sample image

Sample image

Introduction

This is generally an extended version of the auto-resize C# Listbox by Christian Tratz. He didn't have the time to post this version, so I wrote this very short article. The improvements are '\n' support, now and then words got to the right edge, the line (integer) is changed to string to allow custom values in the header. That's it, have fun!

Using the code

See Christian Tratz' article on this, nothing changed. However if you use the Item.Insert instead of Item.Add selected items will now move to the bottom (when something is inserted above, of course). See also the example provided above. In short:

C#
// Create the Listbox..
listbox = new ListBox.MessageListBox();
listbox.Size = new Size( this.ClientSize.Width, this.ClientSize.Height);
listbox.Dock = DockStyle.Top;

// .. and add it to the controls.
this.Controls.Add( listbox);

Then, you can start adding messages to the end of the listbox:

C#
listbox.Items.Add(
 new ListBox.ParseMessageEventArgs(
  ListBox.ParseMessageType.Info,
  "Info1",
  "Some information.. there should be enough text in this area
   to see that the resizing works! The text is wrapped in between
   words as much as possible, since this control has no knowledge
   of words. However, when not possible the words are split."));

Or, insert them at an index:

C#
listbox.Items.Insert(
 1, 
 new ListBox.ParseMessageEventArgs(
  ListBox.ParseMessageType.None,
  "Some more info",
  "Text can also be inserted at any index. Previously selected
   text will e.g. move to the bottom when something is inserted
   above."));

After that, redraw the control:

C#
listbox.Invalidate();

History

  • Version 1.2 - Optimized the drawing method: only the visible items are drawn and measuring only takes place after a resize.
  • Version 1.1 - First Version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionReally Good! Pin
Member 1296452719-Apr-18 22:57
Member 1296452719-Apr-18 22:57 
QuestionHow to Scroll to Last [TopIndex property is missing] Pin
Kaustubh Patange20-Dec-17 2:41
Kaustubh Patange20-Dec-17 2:41 
Questionlicense Pin
flappo01-Jan-14 23:27
flappo01-Jan-14 23:27 
GeneralMy vote of 2 Pin
SercanOzdemir10-Nov-11 0:56
SercanOzdemir10-Nov-11 0:56 
GeneralSystem.ArgumentException Pin
Drunken #BeAvEr#17-Sep-10 12:06
Drunken #BeAvEr#17-Sep-10 12:06 
GeneralImpressive Pin
VCSKicks31-Aug-08 12:56
VCSKicks31-Aug-08 12:56 
GeneralScrolling Issue... Pin
crjackso12-Jun-07 13:48
crjackso12-Jun-07 13:48 
GeneralRe: Scrolling Issue... Pin
foxm2k22-Oct-08 4:19
foxm2k22-Oct-08 4:19 
GeneralA much simpler method Pin
Alan Singfield5-Jul-06 23:27
Alan Singfield5-Jul-06 23:27 
GeneralRe: A much simpler method Pin
Anthony Baraff8-Feb-07 4:18
Anthony Baraff8-Feb-07 4:18 
GeneralRe: A much simpler method Pin
c2j223-May-18 3:36
c2j223-May-18 3:36 
GeneralColumn-Headers for owner drawn listbox Pin
hergru30-Mar-06 2:31
hergru30-Mar-06 2:31 
NewsAdvanced list box and combo box Pin
brett5630-Nov-05 5:50
brett5630-Nov-05 5:50 
GeneralPerformance Enhancements Pin
ilya lozovyy27-Sep-05 4:20
ilya lozovyy27-Sep-05 4:20 
GeneralKeyboard Control Pin
nsxdavid22-Mar-05 11:33
nsxdavid22-Mar-05 11:33 
GeneralPerformance Pin
McZippl26-Jul-04 4:56
McZippl26-Jul-04 4:56 
GeneralRe: Performance Pin
Lelieveld28-Jul-04 1:05
Lelieveld28-Jul-04 1:05 
Questionou est le link? Pin
netclectic22-Jun-04 2:39
netclectic22-Jun-04 2:39 
AnswerRe: ou est le link? Pin
Lelieveld22-Jun-04 2:46
Lelieveld22-Jun-04 2:46 
Indeed Smile | :)

Edited the article..

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.