URGENT: Tab Stops in Combobox

Posted on 2006-04-11
Last Modified: 2008-02-01
I am trying to set some tab stops in a combobox.

Using the following code, I obtain the handle for the list of the combo and attempt to set the tabstops ...

Public Const GWL_EXSTYLE As Long = (-20)
Public Const WS_EX_RIGHT As Long = &H1000
Public Const WS_EX_LEFTSCROLLBAR As Long = &H4000
Public Const CB_SHOWDROPDOWN = &H14F

Public Enum AlignConstants
   alignLeft = 0
   alignRight = 1
End Enum

' *** Declared elsewhere in my mods - unproc to use
'Public Type RECT
'   Left As Long
'   Top As Long
'   Right As Long
'   Bottom As Long
'End Type

   cbSize As Long
   rcItem As RECT
   rcButton As RECT
   stateButton  As Long
   hwndCombo  As Long
   hwndEdit  As Long
   hwndList As Long
End Type

Public Declare Function GetWindowLong Lib "user32" _
   Alias "GetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" _
   Alias "SetWindowLongA" _
  (ByVal hwnd As Long, _
   ByVal nIndex As Long, _
   ByVal dwNewLong As Long) As Long
Public Declare Function GetComboBoxInfo Lib "user32" _
  (ByVal hwndCombo As Long, _

Public Declare Function SendMessage Lib "user32" _
   Alias "SendMessageA" _
  (ByVal hwnd As Long, _
   ByVal wMsg As Long, _
   ByVal wParam As Long, _
   lParam As Any) As Long

Public Const LB_SETTABSTOPS = &H192

Public Sub SetCboTabs(cb As ComboBox)
  'set up the tabstops in the list boxes
   ReDim TabStop(0 To 2) As Long
  'assign some values to the tabs for the second third and fourth
  'column (the first is flush against the listbox edge)
   TabStop(0) = 90
   TabStop(1) = 130
   TabStop(2) = 185
   'Debug.Print GetComboListHandle(cb)
  'clear then set the tabs
   Call SendMessage(GetComboListHandle(cb), LB_SETTABSTOPS, 0&, ByVal 0&)
   Call SendMessage(GetComboListHandle(cb), LB_SETTABSTOPS, 3, TabStop(0))

End Sub

Public Function GetComboListHandle(ctl As ComboBox) As Long


   CBI.cbSize = Len(CBI)
   Call GetComboBoxInfo(ctl.hwnd, CBI)
   GetComboListHandle = CBI.hwndList

End Function

The code was modified from a mvps code - cant remember who or where I got it from. I have never been able to get it to work properly ...

Can anyone can help me get the code to work, or provide alternative coding ... ?

All help appreciated.


Question by:dwe0608
    LVL 28

    Expert Comment

    I don't think you can send a ListBox Message to a ComboBox. Here are some examples from Randy Birch that subclass the combobox to display a listbox or listview instead.
    LVL 28

    Accepted Solution

    Another option would be to use a fixed width font in the combobox and create a Sub routine to add the items to the combobox. Here is a basic example that will add 3 columns to a combobox by checking the items length and padding spaces on the end. The sub will set the combobox box font to Courier. You could adjust that to any fixed width font.

    Private Sub Command1_Click()
    AddComboItem Combo1, "Col 1", "Col 2", "Col 3"
    AddComboItem Combo1, "Column1", "Column2", "Column3"
    AddComboItem Combo1, "Column # 1", "Column  # 2", "Column # 3"
    End Sub

    Private Sub AddComboItem(cBox As ComboBox, Col1 As String, Col2 As String, Col3 As String)
    Dim ColWid1 As Integer, ColWid2 As Integer, ColWid3 As Integer
    Dim Spacer As String
    ' set col width in spaces here
    ColWid1 = 10
    ColWid2 = 20
    ColWid3 = 25

    ' set to a common fixed width font
    cBox.Font = "Courier"

    If ColWid1 > Len(Col1) Then
        Spacer = Space$(ColWid1 - Len(Col1))
        Col1 = Col1 & Spacer
        Col1 = Left$(Col1, ColWid1)
    End If

    If ColWid2 > Len(Col2) Then
        Spacer = Space$(ColWid2 - Len(Col2))
        Col2 = Col2 & Spacer
        Col2 = Left$(Col2, ColWid2)
    End If

    If ColWid3 > Len(Col3) Then
        Spacer = Space$(ColWid3 - Len(Col3))
        Col3 = Col3 & Spacer
        Col3 = Left$(Col3, ColWid3)
    End If

    cBox.AddItem Col1 & Col2 & Col3

    End Sub

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
    Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
    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…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    755 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

    22 Experts available now in Live!

    Get 1:1 Help Now