• Need multiple columns in ComboBox (VB6)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Need multiple columns in ComboBox (VB6)

    Author
    Topic
    #389575

    I am trying to work best I can with the limitation of the ComboBox in VB6. I need to display two categories of data in the drop down and have been trying to write a routine to format everything so it places spaces to try and make the second “column” line up. My problem is it never seems to be consistent. Simply using the Spaces() method and subtracting the length of the length of the string of the second column isn’t working. This is due, I think, to the font pitch being different on every line item.

    I know I can’t be the first one who has needed multiple column capability and was wondering if anyone else has a trick to make the display do a reasonable job of emulating multiple columns?

    Viewing 2 reply threads
    Author
    Replies
    • #688709

      Can you change to a fixed pitch font such as Courier New? Otherwise, the math probably would get pretty intense.

      • #688723

        Can you give me some other choices of fixed fonts? I like the idea, just don’t like Courier New

        • #688767

          Problem is that you can be sure that Courier New is available on every PC – it’s part of every Windows installation, but you can’t be sure about other fixed-width fonts. (If this is just fro your own use, you decide which font to use)

        • #688936

          MS San Serif is fixed. Try that.

          • #688980

            scratch

            • #689162

              Well, the application is running off of a Citrix environment so everyone should have the same fonts available. I’m starting to think that I’m going to have to resort to math. Problem is I’ve already tried that but depending on which order the records are displayed in the drop down, the math doesn’t seem to be applied consistently.

            • #689283

              Okay, it took a REALLY long time to find this, but here’s your entry point into the math. There’s an API call named GetTextExtentPoint32 that returns the length of a string using the character widths stored in the font files.

              Private Type SIZE
                 cx          As Long
                 cy          As Long
              End Type
               
              Private Declare Function GetTextExtentPoint32 Lib "gdi32" _
                  Alias "GetTextExtentPoint32A" _
                 (ByVal hDC As Long, _
                  ByVal lpSz As String, _
                  ByVal cbString As Long, _
                  lpSize As SIZE) As Long

              Some useful pages:

              Windows GDI: GetTextExtentPoint32 (uses C syntax)
              Setting a Command Button’s ForeColor (source for above declarations)
              ActiveX and Visual Basic: Enhance the Display of Long Text Strings in a Combobox or Listbox (esp. Figure 3, showing partial VB syntax)

              Hope this helps, though I can’t say I fully understand it. grin

            • #689510

              Wow, you did do some digging – thanks. Quite honestly, I think it is a bit beyond me, though it does seem to show promise.

    • #689370

      I’ve found it best to use a 3rd party control for multiple columns in combo boxes. There are a selection of controls from BeCubed software that are solid, alternatively you can check out the full source from vbAccelerator for Steve’s owner drawn combo box – I think it has multi-columns.

      http://www.vbaccelerator.com/home/VB/Code/…Full_Source.asp%5B/url%5D

      I still can’t believe this was overlooked in the VB6 combo box, it’s so easy to do in MSForms controls – how hard would it have been for Microsoft to duplicate this??!!

      • #689521

        Thanks, I will check them out. Unfortunately, I am using the Infragistic menu bar control’s combo box so I don’t really have a choice of using a different after-market control. And I agree, and am sure we aren’t the only ones frustrated with Microsoft’s lack of functionality of this control compared to their lesser products. I’ve done some basic math in trying to make the columns line up and I’ve got it real close, but still doesn’t look very professional.

        • #689709

          (Edited by jscher2000 on 27-Jun-03 12:21. Had a bit more fun with the example…)

          I’m probably confused because I work in VBA with MSForms, and not in VB6 with whatever its controls are, but I just wanted to confirm that the built in, multiple-column functionality of the ComboBox isn’t satisfactory for your needs.

          • #689873

            Correct, actually, the functionality is slightly different between VBA and VB. In VBA/Access the ComboBox exposes features such as multiple columns addressable through an arrayed index, column headers, I think even a grid as part of the drop down. In VB, for some reason, they didn’t carry this over and don’t expose any of the features of its VBA counterparts. Your UserForm1 example would be ideal if I could get it to do that with a non fixed width font. Put a highlighted column header on it and it would be perfect.

        • #689749

          Mike,

          Comboboxes on menubars are usually quite different from the built-in controls you would put on a form, and you are, in fact, using an aftermarket control if it comes with the Infragistic control. That behavior is determined by the publisher and what they expose, not by Microsoft.

    • #689820

      I use Tab. Seperate the strings with vbTab instead of Spaces(n).

      myString=Sring1 & vbTab & String2 etc.

      I know this doesn’t give me control over how far the ‘columns’ are apart but for simple lists it’s sufficient for what i want to do.

      Regards,

      Kevin Bell

      • #689874

        I tried this using the following:

        .ComboBox.AddItem Itm.SubItems(1)) & “:” & vbTab & Itm.SubItems(2)

        Though I’m sure ASCII wise it is a tab, but it displays in the ComboBox as one string and represents the vbTab as a box character. It is all run together without actually functioning as a tab.

        • #690080

          Please accept my sincerest apologies.

          A ListBox control will expand tabs, a ComboBox doesn’t appear to do so.

          Regards,

          Kevin Bell

      • #690261

        You can set the tab stops for a standard VB listbox using the Windows LB_SETTABSTOPS message. For this to work the listbox’s Style property must be set to 0 – Standard ( does not work with 1- Checkbox). Simple example: Create new project and add a listbox and command button on form. Add these API declarations to form’s code module:

        Private Declare Function SendMessage Lib “User32” _
        Alias “SendMessageA” _
        (ByVal hWnd As Long, _
        ByVal wMsg As Long, _
        ByVal wParam As Long, _
        lParam As Any) As Long

        Private Const LB_SETTABSTOPS = &H192

        Some sample data to populate listbox:

        Private Sub Form_Load()

        List1.AddItem “January Sales” & vbTab & _
        “February Sales” & vbTab & _
        “March Sales”
        List1.AddItem “50” & vbTab & _
        “500” & vbTab & _
        “5000”
        List1.AddItem “10” & vbTab & _
        “100” & vbTab & _
        “1000”

        End Sub

        To set tab stops:

        Private Sub Command1_Click()

        Dim ListBoxTabs(2) As Long
        Dim n As Long

        ListBoxTabs(1) = 75 ‘ twips
        ListBoxTabs(2) = 150 ‘ twips

        ‘Send LB_SETTABSTOPS message to ListBox:
        n = SendMessage(List1.hWnd, LB_SETTABSTOPS, UBound(ListBoxTabs) + 1, ListBoxTabs(1))

        List1.Refresh
        Erase ListBoxTabs

        End Sub

        (See attached example.) Unfortunately for the problem here, there does not appear to be any analagous message that can be used with the VB ComboBox control. But this might be useful when you want to display “columns” in a listbox at specified intervals.

    Viewing 2 reply threads
    Reply To: Need multiple columns in ComboBox (VB6)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: