Showing forms from Text in Combo Boxes

  • Thread starter Thread starter Daniel Hill
  • Start date Start date
D

Daniel Hill

OK, I have very, VERY basic background knowledge of VB6
and have now upgraded to VB.NET and now I'm struggling to
bring up the forms I want.

What I am looking to do is to have a click a command
button bring up a form, and to have which form is brought
up determined by which item is selected in the
accompanying combo box.

I've gathered that you have to declare the item as a
variable and when you want to call up that you use
Combobox1.valuemember = variable1
And then you can do what you like with it.

However my problem comes when I use more than one
variable (as you do with combo boxes).

It seems that no matter what I do:

Private Sub Butselect_Click_1(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ButSelect.Click

CmbCharacters.ValueMember = item1
frm2.Close()
frm1.Show()
Me.Hide()

CmbCharacters.ValueMember = item2
frm1.Close()
frm2.Show()
Me.Hide()

Or put it in a Select Case or If statement I can't get it
to show the right form.
In this example both result in frm2 being shown.
I just don't get it. Any help would be greatly
appreciated.
 
The first thing I see with the code below is that you're using
"ValueMember" property of the ComboBox, which is a property used when
connecting up a combo box to a datasource. I believe you'll want the
property, "Text" instead -- this will return the text currently shown on
the combo box (ie: as you select different values, the "Test" property
reflects the value currently shown when the dropdown isn't expanded).

Once you have this down, you'll run into another issue... VB.NET is object
oriented in that the form doesn't exist until you create it. In VB6, there
was automatic default instantiantion, so any forms that were part of the
project would automatically be created when you execute the program... In
VB.NET, you have to do this yourself (instanciate the form), which also
gives the added benefit of being able to create multiple instances of the
same form.

The code to create the form would look like:
Dim form1 As New Frm1
form1.Show()

This will create a new form (of type Frm1) and show it to the user. To
close the original form, you'll do something like:
Me.Hide()

NOTE: If you do, "Me.Close()", this will close the current form (destroy
it) and clean up all the resources (any additional forms you've created).

So, in the end, the code would look like (assuming the text in the combo
box are "FRM1" and "FRM2") :

Select Case ComboBox1.Text
Case "FRM1"
Dim form1 As New Frm1
form1.Show()
Me.Hide()
Case "FRM2"
Dim form1 As New Frm2
form1.Show()
Me.Hide()
Case Else
MsgBox("Unknown item selected")
End Select

If you want to use the original form to select between two other forms, the
problem gets more difficult. If you wanted to leave the original form
visible and depending on the combobox, open/close other forms, there's two
ways to do it:
1) Easy way, create a class-level variable to store the form object so you
can close it later
2) Create a collection to hold all the forms you create (so you can create
multiple instances of the same form) and close the ones depending on what's
selected.

I'll show the first one here, let me know if you'd like to see the second.
<g> I'll assume that you're starting with a VB Winforms project, newly
created in VS.
1) Add 2 new forms to the project (File -> Add New Item, and choose
Windows Form in the dialog) with the names, "FRM1" and "FRM2"
2) Drag a combo-box to the form the toolbox
3) Add two items to the combobox (properties window -> Items, click the
"..." and add "FRM1" and "FRM2" on two separate lines)
4) Drag a button to the form from the toolbox
5) Double click on the button to create the event handler
6) Add the following lines of code just after the "Inherits
System.Windows.Forms.Form" line
Dim form1 As Form
Dim form2 As Form
7) Paste the following code in the event handler for the button
Select Case ComboBox1.Text
Case "FRM1"
form1 = New Frm1
form1.Show()
If (form2 Is Nothing) = False Then
form2.Close()
End If
Case "FRM2"
form2 = New Frm2
form2.Show()
If (form1 Is Nothing) = False Then
form1.Close()
End If
Case Else
MsgBox("Unknown item selected")
End Select

Hope this helps! Let me know if you have any other questions,
Pete

--------------------
 
Back
Top