0

我编写了以下代码来增加项目的高度。完成此操作后,在滚动组合时,与普通组合框相比,它以非常高的速度移动。我该如何解决这个问题?

我已将绘图模式设置为 DrawMode.OwnerDrawFixed;

 private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
    {           
        e.DrawBackground();
        System.Diagnostics.Debug.WriteLine(e.State);
        if (e.Index < 0)
        {
            return;
        }       
        SizeF stringMeasure = e.Graphics.MeasureString(this.Items[e.Index].ToString(), e.Font);
        Rectangle rec = new Rectangle(e.Bounds.Left, e.Bounds.Top + ((e.Bounds.Height - ItemHeight)/2 ), e.Bounds.Width, ItemHeight);
        e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, new SolidBrush(this.ForeColor), rec);
        e.DrawFocusRectangle();
    }

    private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
    {
        e.ItemHeight = this.ItemHeight * 2; ;
    }
4

1 回答 1

0

仅当为 OwnerDrawVariable 设置 DrawMode 时才会调用 MeasureItem 事件。

因此,您需要将 DrawMode 更改为 OwnerDrawVariable,或者将 ComboBox 的 ItemHeight 属性设置为this.ItemHeight * 2

不确定滚动速度,这可能只是您的操作系统。我没有注意到太大的区别——你没有使用你的stringMeasure变量,所以你可以把它注释掉。

于 2013-02-22T15:06:52.960 回答