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