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

Resizable ListBox

Rate me:
Please Sign up or sign in to vote.
4.73/5 (10 votes)
26 Jan 20041 min read 75.5K   2.3K   38   5
A standard ListBox redrawing correctly when it is resized.

Image 1

Image 2

Introduction

You know that ListBox is a very useful component for displaying your style list with ownerdraw method. If you are using VariableSize mode to draw ListBox item, I think it will be interesting to you. All works fine if you are not going to change a ListBox's horizontal size. When you try to change ListBox's horizontal size, ListBox calls Draw for each visible item again, but problem is that it is not calling MeasureItems and your ListBox does not look like as you want..

Background

The MeasureItem event is raised when you add a new item to the list. So it occurs only once for each item. I have not found any direct way to call MeasureItem forcedly.

There are some indirect ways to solve this problem.

One of them is full reimplementation of ListBox control. See in the article "An auto-resize C# ListBox" By Christian Tratz.

The second way is to remove all items and add them again; as a result MeasureItems will be recalled for each item again. But if you have many items in your ListBox, this way is not the best one. You may have performance problems.

My solution is that when you need ReMeasuringItem, call code like this:

C#
//  Forced Call MeasureItems.
listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
//

Using the code

Do not put this code in the ListBox resizing event. It will not work. Put it in ListBox's parent control. In my example, I put it in a form's event. For better performance, you may put it in SizeChanged event.

P.S. In my example, I demonstrate resizing ListBox by this way. In your code, you may wait until the form is resized and then allow to redraw ListBox items. Using this way, you will have better performance.

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
Web Developer
Georgia Georgia
I'm 28 My Name is Irakli Lomidze.
I Have 12 Year Exrperiense Development Software under Microsoft.

Desktop Application, Two and Multi tier Application with a rich interface on the client side.

Security Application, Including Cryptography Services for Enterprise Network Users.


Comments and Discussions

 
GeneralMouse Events Pin
imran_shahid@msn.com25-Sep-07 19:17
imran_shahid@msn.com25-Sep-07 19:17 
QuestionAuto Scroll to Bottom Pin
crjackso12-Jun-07 7:47
crjackso12-Jun-07 7:47 
Generaljust call LB_SETITEMHEIGHT Pin
Eugene Polonsky11-Mar-06 6:54
Eugene Polonsky11-Mar-06 6:54 
GeneralLarge Text.. Pin
RK KL12-Nov-05 19:16
RK KL12-Nov-05 19:16 
GeneralRe: Large Text.. Pin
alex143427-Jun-06 16:42
alex143427-Jun-06 16:42 

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.