C# Windows窗体列表框项第一项的高度不为';行不通

C# Windows窗体列表框项第一项的高度不为';行不通,c#,listbox,msdn,C#,Listbox,Msdn,我正在尝试制作一个列表框,其中包含大小可变的项目。从这里可以看出,我有很多工作要做: 但正如你所看到的,第一项似乎被忽略了这里发生了什么以及我如何使其也受ItemHeight属性的影响? 我的代码如下所示: private void ClassicEvent_Load(object sender, EventArgs e) { eventCommands.DrawMode = DrawMode.OwnerDrawVariable; eventCommands.MeasureIt

我正在尝试制作一个列表框,其中包含大小可变的项目。从这里可以看出,我有很多工作要做:

但正如你所看到的,第一项似乎被忽略了这里发生了什么以及我如何使其也受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,但我无法理解。

EventCommands\u MeasureItem
中,显然
measureItemEventTargets e
还包含一个
ItemHeight
属性。使用它确实有效。

如果移动
eventCommands.ItemHeight=48ClassicEvent\u Load
中的项目之前,code>确实有效,但问题是我不能将其用于不同的项目高度。我只是用了48来看看我是否真的能让它工作,但这取决于物品。我猜在第一次调用中有些东西还没有初始化,但我不知道该如何修复它。我试着到处调用Refresh()、Update(),甚至Invalidate(),但运气不好。当你一步一步地执行时,
EventCommands\u DrawItem
是先触发还是
EventCommands\u MeasureItem
?它看起来像是先调用
MeasureItem
6次,然后
DrawItem
6次。在
DrawItem
中设置
ItemHeight
属性也没有任何作用。我猜它首先确定一个项目应该有多高,存储它,然后绘制它。但是我不明白为什么第一项不起作用。无论谁投了反对票,请解释你的理由。如果你不告诉我为什么你认为这是个糟糕的问题,我什么都学不到。