Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleMake a ComboBox's dropdown area wide enough for its choices, allowing for ScaleMode
KeywordsComboBox, ComboBox width
CategoriesControls
 
By Roger Gilchrist.

Use the SendMessage API function to send the ComboBox the CB_SETDROPPEDWIDTH message, specifying the desired width in pixels.

This version saves the ComboBox's parent's ScaleMode value so it can switch to pixels if necessary.

 
Public Sub ComboWidthAdjustment(Cmb As ComboBox)
  'SaveMode added to allow greater safety in case other
  ' parts of code expect different ScaleModes
  'cur_wid added to speed up processing
  'Uses Parent so that only the Combo needs to be passed to
  ' procedure
  Dim I        As Long
  Dim max_wid  As Long
  Dim cur_wid  As Long
  Dim SaveMode As Long

  With Cmb
    'Save Fom's ScaleMode
    SaveMode = .Parent.ScaleMode
    'Reset to vbPixels for TextWidth to work properly
    .Parent.ScaleMode = vbPixels
    'Find longest member
    For I = 0 To .ListCount - 1
      cur_wid = .Parent.TextWidth(.List(I))
      If max_wid < cur_wid Then
        max_wid = cur_wid
      End If
    Next I
    ' Set the width for the dropdown list, adding a margin.
    SendMessage .hwnd, CB_SETDROPPEDWIDTH, max_wid + 10, 0
    'restore form's ScaleMode
    .Parent.ScaleMode = SaveMode
  End With 'Cmb
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated