am Really new in vb and this has been giving heck of worries

i have a combo box with about 13 collections. i want when a user selects an option, it should open in a new form. the problem now is when a 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

Someone should help Asap

Dani AI

Generated

Your Button1_Click is repeatedly changing the ComboBox and then showing a form for every change — that is why all 13 forms open. As suggested, handle the chosen item instead of setting the combo programmatically. Also note 's point about posting in the right forum.

A simple, safe pattern: check the current selection and open only the corresponding form. Put this logic in the ComboBox.SelectedIndexChanged handler or in your button click, but do not change ComboBox.SelectedItem/SelectedValue inside that handler.

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Dim idx = ComboBox1.SelectedIndex
    If idx < 0 Then Exit Sub

    Dim f As Form = Nothing

    Select Case idx
        Case 0
            f = New DetailForm1()
        Case 1
            f = New DetailForm2()
        Case 2
            f = New DetailForm3()
        ' ... continue for each item (0..12 for 13 items)
        Case Else
            Return
    End Select

    If f IsNot Nothing Then
        Me.Hide()
        f.Show()
    End If
End Sub

Quick tips and cautions:

  • SelectedIndex starts at 0. If you added items in a different order, the mapping must match the combo order.
  • If the ComboBox is data-bound, use SelectedValue (and ensure ValueMember is set) instead of SelectedIndex.
  • Prefer creating new instances (New ...) instead of reusing designer instances if you expect multiple opens.
  • If you want the new form modal, use f.ShowDialog() and then Me.Show() after it closes instead of Me.Hide().
  • For maintainability, consider a Type array or Dictionary mapping indices to form Types so you avoid a long Select Case.

Recommended Answers

All 2 Replies

Member Avatar for Member #120589

Reginald - do us a favour and post to the right forum. This is NOT the VB forum - it is a Community Forum. Also you can use more unique tags.

Someone should help Asap

Must be a language thing. Chill out Reginald - post your code questions to the right forum and you may get prompt replies. "ASAP" has no meaning here. We give our time for free - you have no say in how quickly we do it.

Use a select case statement here. As your code is written now it will indeed open all your froms and hide the one you're in.
Follow the advice of diafol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.