Search code examples
javascriptjqueryvb.netcombobox

vb combo box selected item to open in individual form


Am actually new in vb and trying to learn it, i have a combo box of 13 collections and a button, i want whenever a user selects an option from the combo box and clicks the button, it should open in an individual form. but the problem is whenever the user clicks on an option, all the 13 forms will open.

please help here is my code

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ComboBox1.SelectedValue = "1"
    Form2.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "2"
    Form3.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "3"
    Form4.Show()
    ComboBox1.SelectedValue = "4"
    Form3.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "5"
    Form5.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "6"
    Form6.Show()
    ComboBox1.SelectedValue = "7"
    Form7.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "8"
    Form8.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "9"
    Form9.Show()
    ComboBox1.SelectedValue = "10"
    Form9.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "11"
    Form10.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "12"
    Form11.Show()
    Me.Hide()
    ComboBox1.SelectedItem = "13"
    Form12.Show()
    Me.Hide()
End Sub

Solution

  • Based upon what you are trying to do, And what the other members advised you already, It sounds like you did not correctly format your if...then and/or your select...case statements.

    The code below should do what you are trying to do. We use the SELECTINDEX property to find out which index element in the drop down is selected at present.

    Additionally, The code below can be affected (affected so it does not work) by any code in the combobox/dropdown object's event procedures if you have any of them setup.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        '
        Select Case Combobox1.SelectedIndex
            Case 1
                Form2.Show()
                Me.Hide()
            Case 1, 4
                Form3.Show()
                Me.Hide()
            Case 3
                Form4.Show()
                Me.Hide()
            Case 5
                Form5.Show()
                Me.Hide()
            Case 6
                Form6.Show()
                Me.Hide()
            Case 7
                Form7.Show()
                Me.Hide()
            Case 8
                Form8.Show()
                Me.Hide()
            Case 9, 10
                Form9.Show()
                Me.Hide()
            Case 11
                Form10.Show()
                Me.Hide()
            Case 12
                Form11.Show()
                Me.Hide()
            Case 13
                Form12.Show()
                Me.Hide()
        End Select
    '
    End Sub