本文介绍了C#MeasureItemEvent处理程序:e.ItemHeight不变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是我还是只能为列表框设置itemheight-e.ItemHeight?

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

尽管我在我的所有者绘制的列表框上处理MeasureItemEvent并将e.ItemHeight设置为正确的值,但只会使用设置的第一个高度.

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.

糟糕,我对此很陌生,对此感到抱歉.这是代码(DrawItemHandler当然在实际程序中):

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

推荐答案

e.ItemHeight在事件触发时被初始化为ListBox.ItemHeight.它不会保存其先前的设置值.您必须同时修改ListBox.ItemHeight或跟踪变量中的修改.

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.

(在评论之后)

仅当添加新项目时才触发该事件.呼叫ListBox.Refresh()时,所有项目也会触发.

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().

据我了解您的代码,您需要一次增加/减少所有商品的ItemHeight.

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

->我认为您在更新TrackBar时必须调用ListBox.Refresh.

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

在实践中,当列表框收到 WM_MEASUREITEM 仅在首次创建或添加项目时发生.创建列表框及其所有项后,在初始化后,listBox1_MeasureItem内由于刷新而对现有项所做的进一步更改将无用(在Reflector的帮助下可见).

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

我找到了一种在不删除和添加所有项目的情况下将WM_MEASUREITEM强制发送到ListBox的方法:

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;

这篇关于C#MeasureItemEvent处理程序:e.ItemHeight不变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 00:31