[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 188
  • Last Modified:

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

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?

Thanks.
0
jinch
Asked:
jinch
1 Solution
 
GrahamSkanCommented:
This could be a simple answer:

Private Sub Combo1_Change()
    Combo1.ToolTipText = Combo1.Text
End Sub
0
 
ShauliCommented:
'=========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_ERR = -1, CB_SELECTSTRING = &H14D, CB_SHOWDROPDOWN = &H14F, CBN_SELENDOK = 9
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, _
        DT_CALCRECT
   
   lTempWidth = rectCboText.Right - rectCboText.Left + 20

   If (lTempWidth > lWidth) Then
      lWidth = lTempWidth
   End If
Next
 
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
ErrorHandler:
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


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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now