No announcement yet.

Combo Box Text Width

  • Filter
  • Time
  • Show
Clear All
new posts

  • Combo Box Text Width

    I am changing the drop down width of a combo box to fully display the
    longest string in the list with %CB_SETDROPPEDWIDTH. How can I determine
    the width in pixels of the longest string in the list so I know the required
    dropdown width (pixels) to use? Currently I use an arbitrary value (yuk!).
    (Control Send hDlg, %ID_UserID, %CB_SETDROPPEDWIDTH, 200&, 0&)



  • #2
    Ron --
    I like to use in similar situations GetTextExtentPoint32.
    Don't exactly know, how combo box uses DROPPEDWIDTH, but looks logical to set DROPPEDWIDTH as maximum value of for all elements + GetSystemMetrics(%SM_CXHSCROLL) + something for internal borders (2 * GetSystemMetrics(%SM_CXDLGFRAME) ?)

    [This message has been edited by Semen Matusovski (edited June 04, 2000).]


    • #3
      Thanks for the reply, Semen. I rummaged through some books and code
      and pieced together something which appears to work pretty good.

      This function calculates the average width of a character then based
      upon the longest string in the combo box, it sets the dropdown width.

      Function GetDlgUnits(hWnd As Long) As Long
        Local strTemp   As Asciiz * 64
        Local szl       As SIZEL
        Local hFont     As Long
        Local hOldFont  As Long
        Local avgWidth  As Long
        Local hDc       As Long
        hDc = GetDC(hWnd)
        hFont     = GetStockObject(%ANSI_VAR_FONT)
        hOldFont  = SelectObject(hDc, hFont)
        strTemp = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
        GetTextExtentPoint32 hDc, strTemp, 62, szl
        ' 62 = "strTemp" string length
        avgWidth = \ 62
        SelectObject hDc, hOldFont
        DeleteObject hFont
        ReleaseDC hWnd, hDc
        Function = avgWidth
      End Function
        '1 - Determine the maximum length string in the combo box (your own code)
        Local lngLongestCBItem As Long
        lngLongestCBItem = 35  ' use actual calculated length
        avgWidth = GetDlgUnits(hWnd)
        'Add a slight border on the right.  
        'Some strings might require this to be tweaked.  
        'It's all based on average character width  
        NewWidth = ((lngLongestCBItem) * avgWidth) + 1&
        Control Send hwnd, iD, %CB_SETDROPPEDWIDTH, NewWidth,  0&


      • #4
        Ron --
        I told about somethink like this
           #Compile Exe
           #Register None
           #Dim All
           #Include "win32api.INC"
           CallBack Function DlgProc
              Static hFont As Long
              Local El As Asciiz * 256, lpSize As SizeL, cxx As Long, i As Long, _
                  hWndCmb As Long, hdc As Long
              Select Case CbMsg
                 Case %WM_INITDIALOG
                    Control Add ComboBox, CbHndl, 100, , 5, 5, 50, 150, _
                    hWndCmb = GetDlgItem(CbHndl, 100)
                    hDC = GetDC(hWndCmb)
                    hFont = CreateFont(-15, 0, 0, 0, %FW_NORMAL, 0, 0, 0, _
                       %DEFAULT_QUALITY, %FF_DONTCARE, "Arial")
                    SelectObject hDC, hFont
                    SendMessage hWndCmb, %WM_SETFONT, hFont, 0
                    cxx = 0
                    For i = 20 To 1 Step -1
                       El = "# " + Str$(i) + " " + String$(i, "a") + " |"
                       GetTextExtentPoint32 hdc, El, Len(El), lpSize
                       cxx = Max(cxx,
                       ComboBox Add CbHndl, 100, El
                    SendMessage hWndCmb, %CB_SETDROPPEDWIDTH, _
                       cxx + GetSystemMetrics(%SM_CXVSCROLL)+ 2 * GetSystemMetrics(%SM_CXDLGFRAME), 0
                    ReleaseDC hWndCmb, hDC
                 Case %WM_DESTROY
                    DeleteObject hFont
              End Select
           End Function
           Function PbMain
              Local hDlg    As Long
              Dialog New 0, "", ,, 300, 200, %WS_CAPTION Or %WS_SYSMENU To hDlg
              Dialog Show Modal hDlg Call DlgProc
           End Function