By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

Populating Values Based On The Selection

P: 2
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
Apr 4 '13 #1
Share this Question
Share on Google+
4 Replies


100+
P: 158
Hey khomo, write 'Selected Index Changed' event for 'listbox1' (listbox1_SelectedIndexChanged). Inside the event get the current selected value of 'listbox1' using 'listbox1.SelectedItem' property after that you can populate items for 'listbox2' from your datasource.
Apr 4 '13 #2

P: 2
should i put inside the form on the listbox
Apr 8 '13 #3

IronRazer
P: 83
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.
Expand|Select|Wrap|Line Numbers
  1. Public Class Form1
  2.     Dim Subjects() As String = {"Cars", "Colors", "Computers"}
  3.     Dim CarItems() As String = {"Chevy", "Ford", "Dodge"}
  4.     Dim ColorItems() As String = {"Green", "Red", "Blue"}
  5.     Dim ComputerItems() As String = {"Dell", "IBM", "HP"}
  6.  
  7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.         ListBox1.Items.AddRange(Subjects)
  9.     End Sub
  10.  
  11.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
  12.         ListBox2.Items.Clear()
  13.         Select Case ListBox1.SelectedIndex
  14.             Case 0
  15.                 ListBox2.Items.AddRange(CarItems)
  16.             Case 1
  17.                 ListBox2.Items.AddRange(ColorItems)
  18.             Case 2
  19.                 ListBox2.Items.AddRange(ComputerItems)
  20.         End Select
  21.     End Sub
  22. End Class
  23.  
Apr 8 '13 #4

100+
P: 158
Hey khomo, inside the Form load event add the following line.

Expand|Select|Wrap|Line Numbers
  1. listbox1.SelectedIndexChanged += listbox1_SelectedIndexChanged

And then add the following function inside your class.

Expand|Select|Wrap|Line Numbers
  1. Private Sub listbox1_SelectedIndexChanged(sender As Object, e As EventArgs)
  2. Console.WriteLine(listbox1.SelectedItem) // 'listbox1.selectedItem' property have the value of the selected item in listbox1
  3. // 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
  4. End Sub
Apr 8 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.