Click here to Skip to main content
Licence 
First Posted 21 Jun 2004
Views 127,465
Bookmarked 63 times

An auto-resize C# Listbox (updated)

By | 2 Aug 2004 | Article
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:

// 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:

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:

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:

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

About the Author

Lelieveld



Netherlands Netherlands

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 2 PinmemberSercanOzdemir0:56 10 Nov '11  
GeneralSystem.ArgumentException PinmemberDrunken #BeAvEr#12:06 17 Sep '10  
GeneralImpressive PinmemberVCKicks12:56 31 Aug '08  
GeneralScrolling Issue... Pinmembercrjackso13:48 12 Jun '07  
GeneralRe: Scrolling Issue... Pinmemberfoxm2k4:19 22 Oct '08  
GeneralA much simpler method PinmemberAlan Singfield23:27 5 Jul '06  
GeneralRe: A much simpler method PinmemberAnthony Baraff4:18 8 Feb '07  
GeneralColumn-Headers for owner drawn listbox Pinmembergru2best2:31 30 Mar '06  
NewsAdvanced list box and combo box Pinmemberbrett565:50 30 Nov '05  
GeneralPerformance Enhancements Pinmemberilya lozovyy4:20 27 Sep '05  
GeneralKeyboard Control Pinmembernsxdavid211:33 2 Mar '05  
GeneralPerformance PinmemberMcZippl4:56 26 Jul '04  
GeneralRe: Performance PinmemberLelieveld1:05 28 Jul '04  
Questionou est le link? Pinmembernetclectic2:39 22 Jun '04  
AnswerRe: ou est le link? PinmemberLelieveld2:46 22 Jun '04  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Aug 2004
Article Copyright 2004 by Lelieveld
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid