Click here to Skip to main content
15,886,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to use
the Nested If in picking a value from the ComboBox and Display a certain Form

for example

if i choose 1 and click a button
it will show the form1

if i choose 2 and click a button
it will show the form2

I have 13 items

I tried If else for the 2 items and its working fine

but i dont know how to it with 13 items

What I have tried:

If ComboBox1.Text = 1 Then
Form1.Show
Me.close

Else

ComboBox2.Text = 2
Form2.show
me.close

End If

If ComBoBox.Text = "" Then
MessageBox.show("Please Select Number")

End If
Posted
Updated 16-Nov-21 20:10pm

1 solution

That's clumsy: and probably not going to work too well as you almost certainly need to create a new instance of the form in order to display it anyway.

The first improvement is to use switch instead of if:
VB
Dim f As Form = Nothing

Select Case ComboBox1.Text
    Case "1"
        f = New Form1()
    Case "2"
        f = New Form2()
    Case Else
        MessageBox.Show("Please select a number")
        Return
End Select

f.Show()
Me.Close()
But ... be aware that if this is on your main form, then the Close call at the end will immediately terminate the application and close all forms.
 
Share this answer
 
Comments
Beginner213456 17-Nov-21 19:48pm    
Great, its does what i want to do..
Thanks a lot
OriginalGriff 18-Nov-21 1:54am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900