Monday, April 7, 2008

Using Two Forms

Since there is some confusion about how to use two forms, I have made a video to help clear this up. First of all you have to add a second form to your project before you can use it. To do this, go to:

-> Projects
--> Add Windows Form
---> Add

Then in the Solution Explorer [usually top right] you should see two forms. You can double click one of them to begin design on it. So you have to double click ``Form2'' in order to add things to it. You will have to add a listbox to it.

From form1, you have to add one button. You have to program the button so that it will show form2.

When you run the program, only the first form will be shown by default. Therefore, you will have to manually make the second form appear by writing the following code in the button:

Form2.Show()

If you want the first form to disappear, you will have to do so manually by writing the following code:

Me.Hide()

Notice that it does not say ``Form1.Hide().'' This is because a form can only refer to itself using ``Me'' kind of like in English. You wouldn't talk about yourself in the third person, would you?

In order to skip lines you can simply Add nothing to the listbox:

Form2.Listbox1.Items.Add("") 'Skips a line

Since you will be adding several lines, you may want to use the With option:

With Form2.Listbox1.Items
.Add("Business Expense Report")
.Add("")
.Add("Information for Conference")
.Add(txtBox1.Text)
.Add(txtBox2.Text & " in " & txtBox3.Text)
.Add("")
...etc...
End With


Video of Two Forms
You may click on this link, where you can watch it full screen.

No comments: