Results 1 to 3 of 3

Thread: Scroll Bar

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Location
    Hyderabad, India
    Posts
    13

    Scroll Bar

    I have an application where I am not allowd to increase the width of the combo box. Can I have scroll bar in the combo box so that I can see the text in the combo box. ( Pls don't ask me to use tool tip) That is the last solution I have.

    Regards
    Thakur

  2. #2
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    I hope this solution helps. When u click on a combo box, this widens it to display as much text as u want.
    Regards
    Stuart
    VB Code:
    1. 'In module
    2. Public Declare Function SendMessage Lib "USER32" _
    3. Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As _
    4. Long, ByVal wParam As Long, lParam As Long) As Long
    5. Public Const CB_SETDROPPEDWIDTH = &H160
    6.  
    7. Sub SetComboWidth(oComboBox As ComboBox, lWidth As Long)
    8. ' lWidth is in pixels
    9.     SendMessage oComboBox.hWnd, CB_SETDROPPEDWIDTH, lWidth, 0
    10. End Sub
    11.  
    12. 'Form routine example
    13. SetComboWidth combo1, 200
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    If I may be permitted to expand a little on beachbum's reply. This is an combox autosize sub routine that I got from
    http://www.blackbeltvb.com/ (at least I think I got it from there. It may have been somewhere else.)

    Anyway, this routine will check all of the features of the combo box (font size, font style, etc) and automatically resize it to fit the longest entry. You don't need to feed it a number.

    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 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

    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 Function AutosizeCombo(CB As ComboBox) As Boolean

    Dim LongReturn As Long
    Dim CurrentCBWidth As Single
    Dim TheCBItems As RECT
    Dim ParentHdc As Long
    Dim MyListCount As Long
    Dim MyLongCounter As Long
    Dim TempCBWidth As Long
    Dim LongWidth As Long
    Dim SavedFont As String
    Dim SavedSize As Single
    Dim SavedBold As Boolean
    Dim SavedItalic As Boolean
    Dim SavedUnderline As Boolean
    Dim IsFontSaved As Boolean

    On Error GoTo ErrorHandler

    'Grab the combo handle and list count
    ParentHdc = CB.Parent.hdc
    MyListCount = CB.ListCount

    If ParentHdc = 0 Or MyListCount = 0 Then Exit Function

    'Save combo box fonts, etc. to the parent object(form), for testing lengths with the API
    'My personal contribution
    With CB.Parent
    SavedFont = .FontName
    SavedSize = .FontSize
    SavedBold = .FontBold
    SavedItalic = .FontItalic
    SavedUnderline = .FontUnderline
    .FontName = CB.FontName
    .FontSize = CB.FontSize
    .FontBold = CB.FontBold
    .FontItalic = CB.FontItalic
    .FontUnderline = CB.FontUnderline
    End With

    IsFontSaved = True

    'Get the width of the widest item
    For MyLongCounter = 0 To MyListCount
    DrawText ParentHdc, CB.List(MyLongCounter), -1, TheCBItems, DT_CALCRECT
    'Add twenty to the the number as a margin
    TempCBWidth = TheCBItems.Right - TheCBItems.Left + 20
    If (TempCBWidth > LongWidth) Then
    LongWidth = TempCBWidth
    End If
    Next

    'Get current width of combo
    CurrentCBWidth = SendMessageLong(CB.hwnd, CB_GETDROPPEDWIDTH, 0, 0)

    'If big enough then that's all A-OK
    If CurrentCBWidth > LongWidth Then
    AutosizeCombo = True
    GoTo ErrorHandler
    Exit Function
    End If

    '... but if not big enough, first calculate the screen width to ensure we don't exceed it!
    If LongWidth > Screen.Width \ Screen.TwipsPerPixelX - 20 Then _
    LongWidth = Screen.Width \ Screen.TwipsPerPixelX - 20

    'Set the width of our combo
    LongReturn = SendMessageLong(CB.hwnd, CB_SETDROPPEDWIDTH, LongWidth, 0)

    'Set the function to True/False depending on API success
    AutosizeCombo = LongReturn > 0

    ErrorHandler:

    'If anything blows up, reset the combo to its original state
    On Error Resume Next

    If IsFontSaved Then
    With CB.Parent
    .FontName = SavedFont
    .FontSize = SavedSize
    .FontUnderline = SavedUnderline
    .FontBold = SavedBold
    .FontItalic = SavedItalic
    End With
    End If

    End Function

    'In the dropdown even of the combo box, place this code
    'Dim x
    'x = AutosizeCombo(Combo1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width