Listbox of Combo Boxes

Posted on 2010-01-06
Last Modified: 2013-11-26

Is there a way to create a listbox where each item is a combobox in Visual Basic 6?
Question by:NigelRocks
    LVL 14

    Accepted Solution

    Not unless you're a magician. But I have done something similar before with a sneaky workaround.
    First create a subroutine that takes the mouse's x and y locations as parameters. Feed it these parameters with the lisbox's MouseDown event. In the new subroutine, you can calculate the row (and the x, y position) being clicked on, and move an invisible ComboBox (sized just right) on top of that row. Then fill the ComboBox as desired, and then make it visible. Once a selection has been made, you can make the ComboBox invisible again. Play around with this and I'm sure you'll come up with something that looks half decent.
    LVL 1

    Author Comment

    How about a grid where one of the columns contains a combo box?  Is that possible?
    LVL 14

    Assisted Solution

    Yes it is. My "something similar" I did was to move an invisible TextBox on top of the clicked cell of a MSFlexGrid. It gave it an "Excel-like" functionality. And calculating where to place the TextBox (or in your case, a ComboBox) is MUCH easier with a grid. The cell height and width are properties you can use to size the ComboBox.
    With the FlexGrid's MouseUp event (which gives x and y values), I can use these routines:
    Function FlexClickedCol%(fg As MSFlexGrid, x!)
       Rem *** Account for scrolled FlexGrid ***
       For c% = 0 To fg.LeftCol - 1
          pos! = pos! + fg.ColWidth(c%)
       Next c%
       pos! = pos! + x!
       Rem *** Calc entire width **
       For c% = 0 To fg.Cols - 1
          w! = w! + fg.ColWidth(c%)
       Next c%
       Rem *** Find cursor position ***
       For c% = fg.Cols - 1 To 0 Step -1
          w! = w! - fg.ColWidth(c%)
          If pos! >= w! Then
             cx% = c%
             Exit For
          End If
       Next c%
       FlexClickedCol% = cx%
    End Function

    Function FlexClickedRow&(fg As MSFlexGrid, y!)
       If fg.Rows > 2 Then
          Rem *** Get height of header and invisible second row ***
          hdr! = fg.RowHeight(0) + fg.RowHeight(1)
          If y! <= hdr! Then
             rx& = 1
             pos! = hdr!
             Rem *** Account for scrolled FlexGrid ***
             For r& = 2 To fg.TopRow - 1
                pos! = pos! + fg.RowHeight(r&)
             Next r&
             pos! = pos! + y!
             Rem *** Calc entire height **
             h! = hdr!
             For r& = 2 To fg.Rows - 1
                h! = h! + fg.RowHeight(r&)
             Next r&
             Rem *** Find cursor position ***
             For r& = fg.Rows - 1 To 2 Step -1
                h! = h! - fg.RowHeight(r&)
                If pos! >= h! Then
                   rx& = r&
                   Exit For
                End If
             Next r&
          End If
          rx& = 0
       End If
       FlexClickedRow& = rx& - 1
    End Function
    Notice in the above routine that I use an invisible (well, almost...15 twips height) row right after the header row. Don't ask, long story. Anyway, you can take that part out of the hdr! value calculation.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
    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 …
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    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…

    737 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

    20 Experts available now in Live!

    Get 1:1 Help Now