Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Newbie
 
Join Date: Oct 2009
Posts: 4
#1: Oct 15 '09
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's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#2: Oct 16 '09

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


Is both the the controls being populated from database ?
Newbie
 
Join Date: Oct 2009
Posts: 4
#3: Oct 16 '09

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


Quote:

Originally Posted by debasisdas View Post

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...hopefully you can understand what I'm trying to explain. Not good at explaining, really.
Newbie
 
Join Date: Oct 2009
Posts: 19
#4: Oct 16 '09

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


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



=============================
Newbie
 
Join Date: Oct 2009
Posts: 4
#5: Oct 16 '09

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


Quote:

Originally Posted by ggeu View Post

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.selecteditem = "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?
Newbie
 
Join Date: Oct 2009
Posts: 19
#6: Oct 17 '09

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


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


======================================
Newbie
 
Join Date: Oct 2009
Posts: 4
#7: Oct 18 '09

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


Quote:

Originally Posted by ggeu View Post

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 =)
Reply

Tags
combobox, listbox, vb.net, visual basic