Community discussion forum

Creating two related ComboBoxes (VB)

Tags: db Pakistan
  • 1 year ago

    hi ,

     

    I have two (or more) ComboBox controls and I want the options in second combo box to change based on the selected item in the first combo box. 

     When the user changes the value in the country combo box to “Canada”, you would only want to display a list of Canadian provinces/territories in the second combo box. Although if the user selected USA in the country combo box, you would only want to show the American states/regions.?

     

     

     

     

  • 1 year ago

     

    Hello,

    To create related combo boxes, you will have to do some coding in SelectedIndexChanged() event of the Combo box. Every time you select a

    value in Combobox1, this event will fire. If I assume correctly, the values will be fetched from the database for Combobox2. Now in this event, you

    will have to loop through the database to check whether the records contains the name of the Country selected in Combobox1. Add the cities from

    the records containing the selected Country.

    For example:

    BEGIN CODE

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.ComboBox1.Items.Add("United States")

    Me.ComboBox1.Items.Add("Canada")

    Me.ComboBox1.Items.Add("Brazil")

    Me.ComboBox1.Items.Add("Cuba")

    Me.ComboBox1.SelectedIndex = 0

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    Me.ComboBox2.Text = ""

    Me.ComboBox2.Items.Clear()

    If Me.ComboBox1.Text = "United States" Then

    Me.ComboBox2.Items.Add("Washington D.C.")

    ElseIf Me.ComboBox1.Text = "Canada" Then

    Me.ComboBox2.Items.Add("Ottawa")

    ElseIf Me.ComboBox1.Text = "Cuba" Then

    Me.ComboBox2.Items.Add("Havana ")

    ElseIf Me.ComboBox1.Text = "Brazil" Then

    Me.ComboBox2.Items.Add("Brasilia")

    End If

    Me.ComboBox2.SelectedIndex = 0

    End Sub

    END CODE

    I hope this will help. You can use the concept of DataReader to loop through the database records and add the items in the Combobox.

    Regards,

    Allen Smith

    ComponentOne LLC

    www.componentone.com

     

  • 1 year ago

    thank you  its very helpful

     

    but , if I want to get data (cities) from table in database  ?

     

    for example :

    I created table : Table1

    Country : United States,Canada,Brazil

    City : Washington D.C.,Ottawa,Brasilia

    ......

    then , I wrote this code in ComboBox1_SelectedIndexChanged:

    Dim vCon As New SqlConnection

    vCon.ConnectionString = ""........(code for open connection )

    With ComboBox1.SelectedItem

    ComboBox2.Items.Clear()

    Dim vSelectCommand As New SqlCommand

    vSelectCommand.Connection = vCon

    vSelectCommand.CommandText = " Select City From Table1 where Country ='" & ComboBox1.Text & "' "

    Dim vReader As SqlDataReader

    vCon.Open()

    vReader = vSelectCommand.ExecuteReader()

    If vReader.HasRows = True Then

    While vReader.Read

    ComboBox2.Items.Add(vReader(0))

     

    End While

    End If

    End With

    vCon.Close()

    End Sub

    ,,,,,,,,End Code,,,,,,

     

    But , it didn't work correctly !

    I don't know if its right or wrong ?!

    Could you help me and correct my code , please ?

     

    Regards,

  • 1 year ago

     

    Hello,


    I think you have done everything correct.  I have created the same using OLEDB. Basic things remain the same and it works perfectly

    for me. Could you tell me what exactly happens at your end and it will be great if you can provide a small prototype of your project.

    Here is the code that I am using in SelectedIndexChange() event of ComboBox1.

    BEGIN CODE

    Dim dr As OleDbDataReader

    Dim dcs As New OleDbConnection

    Dim dc As New OleDbCommand

    dcs.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\databases\mytable.mdb;User Id=admin;Password=;"

    dc.Connection = dcs

    dc.CommandText = "Select City from Capital where Country='" & ComboBox1.Text & "'"

    dcs.Open()

    dr = dc.ExecuteReader()

    ComboBox2.Items.Clear() 

    If dr.HasRows = True Then

    While dr.Read

    ComboBox2.Items.Add(dr(0))

    End While

    End If

    dcs.Close()

    ComboBox2.SelectedIndex = 0

    END CODE

    Regards,

    Allen Smith

  • 1 year ago

    hi,

    In fact there is no mistake,

    But Nothing Happen
     
    When I set the country , it does not choose any city !! ( Does not show anything in combobox2) !
     
    ....
     
    why you wrote

    ComboBox2.SelectedIndex = 0  ?

    when I wrote it  , the program gives me an error that : InvalidArgument=Value of '0' is not valid for 'SelectedIndex'.
    Parameter name: SelectedIndex.

     

     

    I hope you will find solution :)

    THANK YOU

     

    Regards,

     

     
  • 1 year ago

     

    Hello,

    By default, when we add items to combo box, it does not show any thing in its Text area till the time we select

    any option from the dropdown. To make a default selection, we use SelectedIndex and set its value to index of

    the item from the dropdown list where the indexing begins from 0.

    Now I would like to know whether the data is getting fetched using the DataReader you are using. From the error message

    you have provided, it seems that no data is being getting added into the Combo box. To verify this, put a message box to

    show the data instead of adding it to the Combo box. Let me know the results on this.

    Regards,

    Allen Smith

  • 1 year ago

    Hello,

    I think the data is getting fetched using the DataReader :

    I tried this :

    vSelectCommand.CommandText = " Select City From Table1 "

     when I choose any country ,  I got all the cities added  into Combobox2 .

    But ,

    When I write WHERE statment , No data is adding into Combobox2 !

     

    Regards,

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback