views:

328

answers:

1

Hi All-

Is it me or can I only set the itemheight - e.ItemHeight - once for a listbox?

Although I handle the MeasureItemEvent on my ownerdrawn listbox and set the e.ItemHeight to the right value, only the first height that is set will be used.

Oops, I am new to this, sorry about that. This is the code (DrawItemHandler is of course in the actual program):

// Add eventhandler to draw and measure items
this.listBox1.DrawItem += new DrawItemEventHandler(this.DrawItemHandler);
this.listBox1.MeasureItem += new MeasureItemEventHandler(this.MeasureItemHandler);

// The eventhandler itself
private void MeasureItemHandler(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = Convert.ToInt32(mySettings.iCurrentSizeFactor * 10) + 1;
}
A: 

Hello,

e.ItemHeight is initialized to ListBox.ItemHeight in the event firing. It does not save it's previously set value. You have to either modifiy ListBox.ItemHeight along, or keep track of your modifications in a variable.


EDIT: (following the comment)

The event is fired when a new item is added and only for that item. It's fired also for all items when you call ListBox.Refresh().

As I understand your code, you need to increase/decrease ItemHeight for all your items at once.

--> I think you have to call ListBox.Refresh when you update the TrackBar.


EDIT 2:

In practice, items height is changed when the ListBox receives WM_MEASUREITEM which only happens when it's first created, or when an item is added. After the creation of the ListBox and all of its items at initialization, further changes within listBox1_MeasureItem for existing items due to a refresh are useless (seen with the help of Reflector).

I found a way to force a WM_MEASUREITEM to be sent to the ListBox without deleting and adding all items:

In place of ListBox.Refresh(), put:

ListBox.DrawMode = DrawMode.Normal;
ListBox.DrawMode = DrawMode.OwnerDrawVariable;
najmeddine
The idea is to take the standard height and multiply it with a sizeFactor. Default_Height * Size_Factor = Desired_Height. The sizefactor is controlled with a trackbar so the user can either increase or decrease the font size. Does that make more sense?
ErikSpierenburg
I do call ListboxRefresh(), the event is fired fine, the changed size - e.ItemHeight - does not get set. Expected behaviour is that once you handle the MeasureItem event I should be able to set the e.ItemHeight, no? (I am sure I am missing something, but what?)
ErikSpierenburg
That is it! Thanks, it works perfect now, thanks najmeddine (and all others that helped)!
ErikSpierenburg
so now you can accept my answer )
najmeddine
Done, thanks for the heads up, I could get used to this ;)
ErikSpierenburg