I want to create an application where the user select the category in the first list box and automatically list box 2 gets populated with data relating to each category selected in list box one
Populating Values Based On The Selection
Collapse
X
-
Hey khomo, write 'Selected Index Changed' event for 'listbox1' (listbox1_Selec tedIndexChanged ). Inside the event get the current selected value of 'listbox1' using 'listbox1.Selec tedItem' property after that you can populate items for 'listbox2' from your datasource. -
Here is a simple example. I used 2 listboxes on a new form and instead of a database i used string arrays to hold the data. When you select an item in listbox1 it populates listbox2 with the items that belong to the selected subject. You could also put the (Select Case) in the MouseDown or MouseUp events of the listbox if you want.
Code:Public Class Form1 Dim Subjects() As String = {"Cars", "Colors", "Computers"} Dim CarItems() As String = {"Chevy", "Ford", "Dodge"} Dim ColorItems() As String = {"Green", "Red", "Blue"} Dim ComputerItems() As String = {"Dell", "IBM", "HP"} Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ListBox1.Items.AddRange(Subjects) End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged ListBox2.Items.Clear() Select Case ListBox1.SelectedIndex Case 0 ListBox2.Items.AddRange(CarItems) Case 1 ListBox2.Items.AddRange(ColorItems) Case 2 ListBox2.Items.AddRange(ComputerItems) End Select End Sub End Class
Comment
-
Hey khomo, inside the Form load event add the following line.
Code:listbox1.SelectedIndexChanged += listbox1_SelectedIndexChanged
And then add the following function inside your class.
Code:Private Sub listbox1_SelectedIndexChanged(sender As Object, e As EventArgs) Console.WriteLine(listbox1.SelectedItem) // 'listbox1.selectedItem' property have the value of the selected item in listbox1 // Now connect to your database and retrive all the records which are related to the selected item in listbox1 and fetch those values in listbox2 End Sub
Comment
Comment