Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

.net - Unable to set the DropDownHeight of ComboBox

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

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.

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.

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

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.

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

EDIT:
Would switching to WPF make this problem go away? (Is there enough customizability in the standard WPF controls so that I don't need to write a custom control for a 3-column, variable-height combobox?)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...