tags:

views:

514

answers:

3

Fairly straight forward question, is it possible to align text (left, right, center etc...) in comboboxes in VB6? If so, how?

When the text is longer than the width of a combobox, what is shown of the text starts somewhere in the middle of the text.

+1  A: 

The answer unfortunately is not so straightforward.

There is a code example here: http://vbnet.mvps.org/index.html?code/comboapi/comborightalignstylebits.htm that will right align the list items in a combo box. There is no example of centering the text however.

Patrick McDonald
In the end, I opted for another solution: avoiding text that is longer than than the combobox and just making it wider =) KISS solution more or less, since the combobox was tiny to begin with...
Johan
A: 

Here's some code a friend of mine wrote for autosizing the combo box to it's contents.

Code

Public Sub ComboDropDownWidth(ByRef cboThis As ComboBox, _
                              ByVal lWidth As Long)
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
    'PS 11/05/06 PUK5023 This function will custom re-size a combo drop-down width (see also ComboDropDownWidthFromContents)
    On Error GoTo ComboDropDownWidth_Err
    SendMessageLong cboThis.hWnd, CB_SETDROPPEDWIDTH, lWidth, 0
Exit_Point:
    Exit Sub
    '' Error Handling
ComboDropDownWidth_Err:
    LogError "modNYFixLibrary.ComboDropDownWidth", Err.Number, Err.Description
    GoTo Exit_Point
    Resume
End Sub
Public Sub ComboDropDownWidthFromContents(ByRef cboThis As ComboBox, _
                                          Optional ByVal lMaxWidth = -1)
    'PS 11/05/06 PUK5023 Added to automatically size a combo drop-down width to the widest entry
    Dim i As Long
    Dim tR As RECT
    Dim lW As Long
    Dim lWidth As Long
    Dim lHDC As Long
    On Error GoTo ComboDropDownWidthFromContents_Err
    ' Cache the HDC of the parent form for speed:
    lHDC = cboThis.Parent.HDC
    ' Loop through each combo box list item & get its
    ' width, storing the largest:
    For i = 0 To cboThis.ListCount - 1
        DrawText lHDC, cboThis.List(i), -1, tR, DT_CALCRECT
        lW = tR.Right - tR.Left + 8
        If (lW > lWidth) Then
            lWidth = lW
        End If
    Next i
    ' Don't allow width to exceed specified max
    ' width, or the width of the screen:
    If lMaxWidth <= 0 Then
        lMaxWidth = Screen.Width \ Screen.TwipsPerPixelX - 16
    End If
    If (lWidth > lMaxWidth) Then
        lWidth = lMaxWidth
    End If
    ' Combo box looks a bit strange when the
    ' drop down portion is smaller than the
    ' combo box itself:
    If (lWidth < cboThis.Width \ Screen.TwipsPerPixelX) Then
        lWidth = cboThis.Width \ Screen.TwipsPerPixelX
        ComboDropDownWidth cboThis, lWidth
    Else
        'If it is longer Set the drop down width and add a little for good measure otherwise it still obscures the last character
        ComboDropDownWidth cboThis, lWidth + 20
    End If
Exit_Point:
    Exit Sub
    '' Error Handling
ComboDropDownWidthFromContents_Err:
    LogError "modNYFixLibrary.ComboDropDownWidthFromContents", Err.Number, Err.Description
    GoTo Exit_Point
    Resume
End Sub
MaSuGaNa
Can someone please edit this to format the contents, I can never get it to accept formatted code in VB6!
MaSuGaNa
Looks quite similar to the code I've linked to from vbAccelerator.com (right down to the comments!). But with a bug - I think this code won't work when the combo box has a different font to the parent form.
MarkJ
Ha! I'll happily accuse my mate over email for the code steal!
MaSuGaNa
A: 

Here's a link to VB6 code for automatically sizing the drop-down portion of a combo-box to fit the contents.

vbAccelerator.com usually has high quality code. Interestingly the vbAccelerator code looks very similar to the code in this answer (right down to the comments), but the vbAccelerator code works when the combo box has a different font to its parent form, unlike the code in the other answer.

MarkJ