Select an item from combo box then display to list box?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • naimasora
    New Member
    • Oct 2009
    • 4

    Select an item from combo box then display to list box?

    I have a combobox and a listbox. When the user select an item from the combobox, then the listbox will show a certain data. How do I do this?

    Thank you in advance.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    Is both the the controls being populated from database ?

    Comment

    • naimasora
      New Member
      • Oct 2009
      • 4

      #3
      Originally posted by debasisdas
      Is both the the controls being populated from database ?
      No. It's like this:

      I have a combobox that shows two options: for example, maybe day and night. If the user selects day, a certain item is going to be displayed in the list box, but they are not from database. It's gonna be in the coding. It sounds simple...hopefu lly you can understand what I'm trying to explain. Not good at explaining, really.

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        dear,

        like this:?

        form with combobox1 and listbox1

        =============== ============
        Option Explicit


        Private Sub Form_Load()
        Dim i As Integer
        For i = 1 To 9
        Combo1.AddItem "item " & i
        Next
        End Sub

        Private Sub Combo1_Click()
        List1.AddItem (Combo1.Text)
        End Sub



        =============== ==============

        Comment

        • naimasora
          New Member
          • Oct 2009
          • 4

          #5
          Originally posted by ggeu
          dear,

          like this:?

          form with combobox1 and listbox1

          =============== ============
          Option Explicit


          Private Sub Form_Load()
          Dim i As Integer
          For i = 1 To 9
          Combo1.AddItem "item " & i
          Next
          End Sub

          Private Sub Combo1_Click()
          List1.AddItem (Combo1.Text)
          End Sub



          =============== ==============
          No, but close.

          Actually the items of the combobox was determined in the design of the form, so when user clicked on an item from the combobox, then something will appear on the listbox. This is what I have in mind:

          =============== =============== ====
          'courts is one of the item of the combobox.
          If combobox.select editem = "courts" Then
          lstbox = "I'm going to add something here."

          =============== =============== ====

          obviously my code is wrong. But is it possible to do this kind of thing?

          Comment

          • Guido Geurs
            Recognized Expert Contributor
            • Oct 2009
            • 767

            #6
            dear,

            You have to make the link from the value in the combobox to the value in the listbox.
            You can do this with a array in the background.
            Like: each string in this array has 2 data separeted with a char that not can be present in the used data like= # or | or ...
            I have used # in the example.
            You can load the data from inside the form or better: from a file with the data strings (data is better to modify afterwards).

            When You select a value in the combo, it looks in the array for the LEFT part of the string and puts the RIGHT part in the listbox.

            Create a form with combobox1 and listbox1

            code=
            =============== =============== ========
            Option Explicit
            '§ declare a 1 dimensional array
            Dim table() As String

            Private Sub Form_Load()
            Dim i As Integer
            '§ fill the array with string= "number 1#value = 1", "number 2#value = 2", ...
            For i = 1 To 9
            ReDim Preserve table(i) As String
            table(i) = "number " & i & "#" & "value = " & i
            '§ fill the combobox with the LEFT string to char "#"= "number 1", "number 2", ...
            Combo1.AddItem Left(table(i), InStr(table(i), "#") - 1)
            Next
            End Sub

            Private Sub Combo1_Click()
            Dim i As Integer
            '§ seek in the table for the LEFT string in the combo selection and
            '§ put the string right of the "#" in the listbox ="value = 1" OR "value = 2" OR ...
            For i = 1 To UBound(table)
            If Left(table(i), InStr(table(i), "#") - 1) = Combo1.Text Then _
            List1.AddItem Mid(table(i), InStr(table(i), "#") + 1)
            Next
            End Sub


            =============== =============== ========

            Comment

            • naimasora
              New Member
              • Oct 2009
              • 4

              #7
              Originally posted by ggeu
              dear,

              You have to make the link from the value in the combobox to the value in the listbox.
              You can do this with a array in the background.
              Like: each string in this array has 2 data separeted with a char that not can be present in the used data like= # or | or ...
              I have used # in the example.
              You can load the data from inside the form or better: from a file with the data strings (data is better to modify afterwards).

              When You select a value in the combo, it looks in the array for the LEFT part of the string and puts the RIGHT part in the listbox.

              Create a form with combobox1 and listbox1

              code=
              =============== =============== ========
              Option Explicit
              '§ declare a 1 dimensional array
              Dim table() As String

              Private Sub Form_Load()
              Dim i As Integer
              '§ fill the array with string= "number 1#value = 1", "number 2#value = 2", ...
              For i = 1 To 9
              ReDim Preserve table(i) As String
              table(i) = "number " & i & "#" & "value = " & i
              '§ fill the combobox with the LEFT string to char "#"= "number 1", "number 2", ...
              Combo1.AddItem Left(table(i), InStr(table(i), "#") - 1)
              Next
              End Sub

              Private Sub Combo1_Click()
              Dim i As Integer
              '§ seek in the table for the LEFT string in the combo selection and
              '§ put the string right of the "#" in the listbox ="value = 1" OR "value = 2" OR ...
              For i = 1 To UBound(table)
              If Left(table(i), InStr(table(i), "#") - 1) = Combo1.Text Then _
              List1.AddItem Mid(table(i), InStr(table(i), "#") + 1)
              Next
              End Sub


              =============== =============== ========
              Thanks for this! I think I will try and do this later, hopefully it will work. (I have so many assignments to finish!)

              Thank you for your help, everyone =)

              Comment

              Working...