c# MeasureItemEvent handler: e.ItemHeight not changing

﹥>﹥吖頭↗ 提交于 2019-12-11 15:33:39

问题


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;
}

回答1:


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;


来源:https://stackoverflow.com/questions/1420674/c-sharp-measureitemevent-handler-e-itemheight-not-changing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!