无法设置 ComboBox 的 DropDownHeight [英] Unable to set the DropDownHeight of ComboBox

查看:14
本文介绍了无法设置 ComboBox 的 DropDownHeight的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法正确设置 ComboBox 的 DropDownHeight 以显示所有项目.

I cannot get the DropDownHeight of the ComboBox set properly to display all the items.

我正在使用从 ComboBox 继承的控件.我已经覆盖了 OnDrawItem 和 OnMeasureItem 方法,以便在需要时在列中创建多列和文本换行.这一切正常.

I am using a control that inherits from the ComboBox. I have overridden the OnDrawItem and OnMeasureItem methods in order to create multiple columns and text-wrapping within a column if it is required. This all works fine.

当我尝试设置 DropDownHeight 时出现问题.我将 DropDownHeight 设置为一个任意大的值,比项目列表大一点.ComboBox 控件似乎会自动截断任何大于列表中所有显示项目大小的 DropDownHeight 值.(假设您将 MaxDropDownItems 属性设置为高于项目数,我就是这样做的.)通常这种行为可以完美运行,如下所示:替代文字 http://www.freeimagehosting.net/uploads/dd09404697.png

The problem occurs when I try to set the DropDownHeight. I set the DropDownHeight at an arbitrarily large value, a good bit larger than the list of items. The ComboBox control appears to automatically truncate any value for DropDownHeight that is larger than the size of all the displayed items in the list. (Assuming that you have the MaxDropDownItems property set higher than the number of items, which I do.) Normally this behavior works perfectly, as shown below: alt text http://www.freeimagehosting.net/uploads/dd09404697.png

不,下拉框中的不是我的真实数据.

No, that's not my real data in the drop-down box.

当我在下拉列表中有一个条目需要换行以显示全文时,就会出现问题.该条目显示正常,但是 ComboBox 正在计算 DropDownHeight,它忽略了一个条目是正常高度两倍的事实,因此您必须向下滚动一行才能到达下拉列表中的最后一个条目.替代文字 http://www.freeimagehosting.net/uploads/d0ef715f83.png

The problem occurs when I have an entry in the drop-down that needs to wrap in order to display the full text. This entry displays fine, but however the ComboBox is calculating the DropDownHeight, it ignores the fact that one of the entries is twice as tall as normal, so you have to scroll down one line to get to the last entry in the drop-down. alt text http://www.freeimagehosting.net/uploads/d0ef715f83.png

这是我用来确定一个项目是否需要文字环绕和设置每个项目的高度的代码:

This is the code that I am using to determine if an item needs text wrapping and to set the height of each item:

 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)) + 1
    e.ItemHeight = e.ItemHeight * HeightMultiplier

 End Sub

我无法确定如何强制 DropDownHeight 属性恰好为我想要的值,或者如何让 ComboBox 控件知道列表中的一个(或多个)项目比正常值高.

I cannot determine how to force the DropDownHeight property to be exactly the value that I want, or how to let the ComboBox control know that one (or more) of the items in the list are taller than normal.

我已经尝试覆盖隐藏 DropDownHeight 属性,但这似乎没有影响.

I've tried to Override Shadow the DropDownHeight property, but this seemed to have no impact.


切换到 WPF 会让这个问题消失吗?(标准 WPF 控件是否具有足够的可定制性,以便我不需要为 3 列可变高度组合框编写自定义控件?)

推荐答案

我正在尝试自己解决这个完全相同的问题,目前我正在从 VB6 迁移到 VB.NET 的应用程序.我在 VB6 中的自绘组合控件通过 SetWindowPos API 调用设置下拉的高度以响应组合控件上的 WM_CTLCOLORLISTBOX 消息,这使我们可以访问组合下拉列表的 HWnd控制.以下代码已添加到我从 ComboBox 继承的类中,似乎可以解决问题,但仍需要测试.我也不确定这是否是最优雅的方式.显然,您需要更改设置 newHeight 变量的行,但这应该可以让您大致了解.

I'm trying to solve this exact same problem myself right at the moment for an application that I am migrating from VB6 to VB.NET. The owner-drawn combo control I have in VB6 sets the height of the drop-down through a SetWindowPos API call in response to the WM_CTLCOLORLISTBOX message on the combo control, which gives us access to the HWnd for the drop-down list of the combo control. The following code was added to my class that inherits from ComboBox and seems to do the trick, but still needs testing. I'm not sure it's the most elegant way of doing this either. Obviously you'll need to change the line that sets the newHeight variable, but this should give you the general idea.

Private Structure RECT
    Public Left As Integer        'x position Of upper-left corner
    Public Top As Integer         'y position Of upper-left corner
    Public Right As Integer       'x position Of lower-right corner
    Public Bottom As Integer      'y position Of lower-right corner
End Structure

Private Declare Function GetWindowRect Lib "user32" _
        (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer

Private Declare Sub SetWindowPos Lib "user32" _
        (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, _
        ByVal X As Integer, ByVal Y As Integer, _
        ByVal cx As Integer, ByVal cy As Integer, _
        ByVal wFlags As Integer)

Private Const SWP_NOZORDER As Integer = &H4
Private Const SWP_NOACTIVATE As Integer = &H10
Private Const SWP_FRAMECHANGED As Integer = &H20
Private Const SWP_NOOWNERZORDER As Integer = &H200

Private _hwndDropDown As Integer = 0

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
    Const WM_CTLCOLORLISTBOX As Integer = &H134

    If m.Msg = WM_CTLCOLORLISTBOX Then
        If _hwndDropDown = 0 Then
            _hwndDropDown = m.LParam.ToInt32

            Dim r As RECT
            GetWindowRect(m.LParam.ToInt32, r)

            'height of four items plus 2 pixels for the border in my test
            Dim newHeight As Integer = 4 * MyBase.ItemHeight + 2

            SetWindowPos(m.LParam.ToInt32, 0, _
                         r.Left, _
                         r.Top, _
                         MyBase.DropDownWidth, _
                         newHeight, _
                         SWP_FRAMECHANGED Or _
                                SWP_NOACTIVATE Or _
                                SWP_NOZORDER Or _
                                SWP_NOOWNERZORDER)
        End If
    End If

    MyBase.WndProc(m)
End Sub

Protected Overrides Sub OnDropDownClosed(ByVal e As System.EventArgs)
    _hwndDropDown = 0
    MyBase.OnDropDownClosed(e)
End Sub

这篇关于无法设置 ComboBox 的 DropDownHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆