我无法正确设置ComboBox的DropDownHeight以显示所有项目.
我正在使用从ComboBox继承的控件.我已经重写了OnDrawItem和OnMeasureItem方法,以便在需要时在列中创建多个列和文本换行.一切正常.
当我尝试设置DropDownHeight时会出现问题.我将DropDownHeight设置为一个任意大的值,比项目列表大一点.ComboBox控件似乎自动截断DropDownHeight的任何值,该值大于列表中所有显示项的大小.(假设您将MaxDropDownItems属性设置为高于项目数,我这样做.)通常这种行为完美无缺,如下所示: alt text http://www.freeimagehosting.net/uploads/dd09404697.png
不,这不是我在下拉框中的真实数据.
当我在下拉列表中有一个条目需要换行以显示全文时,会出现问题.此条目显示正常,但是ComboBox正在计算DropDownHeight,它忽略了其中一个条目是正常值的两倍这一事实,因此您必须向下滚动一行才能到达下拉列表中的最后一个条目. alt text http://www.freeimagehosting.net/uploads/d0ef715f83.png
这是我用来确定项目是否需要文本换行以及设置每个项目的高度的代码:
Protected Overrides Sub OnMeasureItem(ByVal e As System.Windows.Forms.MeasureItemEventArgs)
MyBase.OnMeasureItem(e)
//Determine the proper height of the current row in the dropdown based on
//the length of the OptionDescription string.
Dim tmpStr As String = FilterItemOnProperty(Items(e.Index), "OptionDescription")
Dim lng As Single = e.Graphics.MeasureString(tmpStr, Me.Font).Width
//Use the length of the item and the width of the column to calculate if wrapping is needed.
Dim HeightMultiplier As Integer = Math.Floor(lng / _ColumnWidths(1)) + …Run Code Online (Sandbox Code Playgroud)