【发布时间】:2018-02-07 11:04:03
【问题描述】:
我正在尝试制作一个包含可变大小项目的 ListBox。我有那么多工作要做,如下所示:
但是正如你所看到的,第一项似乎被忽略了。 这里发生了什么以及我如何使它也受到 ItemHeight 属性的影响? 我的代码如下所示:
private void ClassicEvent_Load(object sender, EventArgs e)
{
eventCommands.DrawMode = DrawMode.OwnerDrawVariable;
eventCommands.MeasureItem += EventCommands_MeasureItem;
eventCommands.DrawItem += EventCommands_DrawItem;
eventCommands.Items.Add("Text: I heard you were going to the Pokémon Centre.");
eventCommands.Items.Add("if (Has Pokemon (Bulbasaur) in PC)");
eventCommands.Items.Add(" Text: You have a Bulbasaur.");
eventCommands.Items.Add("else");
eventCommands.Items.Add(" Text: You don't have a Bulbasaur.");
eventCommands.Items.Add("end");
eventCommands.Items.Add("Text: So be it, then.");
}
private void EventCommands_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.Graphics.DrawString(eventCommands.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
e.DrawFocusRectangle();
}
private void EventCommands_MeasureItem(object sender, MeasureItemEventArgs e)
{
eventCommands.ItemHeight = 48;
}
我已经尝试更改 e.Bounds 矩形,并且如果迭代的项目是第一个,还尝试更改 ItemHeight,但我无法弄清楚。
【问题讨论】:
-
如果在添加
ClassicEvent_Load中的项目之前移动eventCommands.ItemHeight = 48;会发生什么? -
这确实有效,但问题是我不能将它与不同的 ItemHeight 一起使用。我只是用 48 来看看我是否真的可以让它工作,但这取决于项目。我猜第一次调用还没有初始化一些东西,但我不知道如何解决这个问题。我尝试在这里和那里调用 Refresh()、Update() 甚至 Invalidate(),但没有成功。
-
单步执行时,
EventCommands_DrawItem是先触发还是EventCommands_MeasureItem? -
看起来它首先调用了
MeasureItem6 次,然后调用了DrawItem6 次。并且在DrawItem中设置ItemHeight属性也没有任何作用。我猜它首先确定项目应该具有的高度,存储它并 然后 绘制它。但后来我不明白为什么它不适用于第一项。 -
谁投了反对票,请解释你的理由。如果你不告诉我为什么你认为这是一个糟糕的问题,我什么也学不会。