To show the combo box text that is longer than combo box width

Posted on 2006-05-09
Last Modified: 2010-08-05
I have a data entry form which requires a lot of information. so I have many controls in the form and I have to make combo boxes small but the text in the combo boxes are long. Style property of the combo boxes is Dropdown List.
How can users see entire text in the combo box?

Question by:jinch
    LVL 76

    Accepted Solution

    This could be a simple answer:

    Private Sub Combo1_Change()
        Combo1.ToolTipText = Combo1.Text
    End Sub
    LVL 19

    Expert Comment

    '=========In a module ===============
    Option Explicit
    Private Declare Function SendMessageLong Lib "user32" Alias _
            "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
            ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Const CB_SETDROPPEDWIDTH = &H160
    Private Const CB_GETDROPPEDWIDTH = &H15F
    Private Const DT_CALCRECT = &H400

    Private Type RECT
       Left As Long
       Top As Long
       Right As Long
       Bottom As Long
    End Type

    Private Declare Function DrawText Lib "user32" Alias _
        "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, _
        ByVal nCount As Long, lpRect As RECT, ByVal wFormat _
        As Long) As Long

    Public Function AutoReSize_ComboBox(Combo As Object) As Boolean
    Dim lRet As Long, bAns As Boolean, lCurrentWidth As Single, rectCboText As RECT, lParentHDC As Long
    Dim lListCount As Long, lCtr As Long, lTempWidth As Long, lWidth As Long, sSavedFont As String
    Dim sngSavedSize As Single, bSavedBold As Boolean, bSavedItalic As Boolean, bSavedUnderline As Boolean
    Dim bFontSaved As Boolean

    On Error GoTo ErrorHandler

    If Not TypeOf Combo Is ComboBox Then Exit Function
    lParentHDC = Combo.Parent.hdc
    If lParentHDC = 0 Then Exit Function
    lListCount = Combo.ListCount
    If lListCount = 0 Then Exit Function

    With Combo.Parent

        sSavedFont = .FontName
        sngSavedSize = .FontSize
        bSavedBold = .FontBold
        bSavedItalic = .FontItalic
        bSavedUnderline = .FontUnderline
        .FontName = Combo.FontName
        .FontSize = Combo.FontSize
        .FontBold = Combo.FontBold
        .FontItalic = Combo.FontItalic
        .FontUnderline = Combo.FontItalic

    End With

    bFontSaved = True

    For lCtr = 0 To lListCount
       DrawText lParentHDC, Combo.List(lCtr), -1, rectCboText, _
       lTempWidth = rectCboText.Right - rectCboText.Left + 20

       If (lTempWidth > lWidth) Then
          lWidth = lTempWidth
       End If
    lCurrentWidth = SendMessageLong(Combo.hwnd, CB_GETDROPPEDWIDTH, _
        0, 0)
       If lWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
        lWidth = Screen.Width \ Screen.TwipsPerPixelX - 20

    lRet = SendMessageLong(Combo.hwnd, CB_SETDROPPEDWIDTH, lWidth + 10, 0)

    AutoReSize_ComboBox = lRet > 0
    On Error Resume Next
    If bFontSaved Then
      With Combo.Parent
        .FontName = sSavedFont
        .FontSize = sngSavedSize
        .FontUnderline = bSavedUnderline
        .FontBold = bSavedBold
        .FontItalic = bSavedItalic
     End With
    End If
    End Function

    '============ End module ===============

    '============ In form Load event ===========

    Private Sub Form_Load()
    Combo1.AddItem "Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla Bla "
    Combo1.AddItem "Bla Bla Bla Bla "
    AutoReSize_ComboBox Combo1
    End Sub


    Author Comment

    My combo boxes are an array and tooltiptext is not working on Change event but it worked on Click event.
    thank you.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now