You are on page 1of 4

Software Development (VB.

NET)

12

Multiple Forms

Objectives for this unit


After studying this unit you will be able to:

Create a new form Reference one form from another Share data between forms

Page 1 of 4

Software Development (VB.NET)

The Form Class


In VB.Net when you create a windows application, the application is created with an already created Form1. This Form1 is a class. It is a more complicated class than those you have met so far but it is nevertheless a Class. From the previous lecture you have learnt that a class is a template and cannot normally be executed as such. However you can create an object instance of a class and execute that. So you might be wondering how is it that when you create a windows application and execute it, it creates an executable form at run time, and yet you havent done anything about creating an Form1 type object in the program. The answer is that the .NET framework has created a startup object for you at run time. The start up property is specified in the Startup Form Combo of the Project Property Pages.

A number of points. 1. 2. 3. 4. The start up object can be either a form or sub Main in a module. The default is Form1. If you rename Form1 you need to re specify the start up object. If VB.NET cannot find the start up object it will try to start sub Main

Page 2 of 4

Software Development (VB.NET)

Creating and using a second Form


Start by creating a new form, with a single button on it, which will be used to open another form.

To create a second form you need to click on Project | Add Windows Form. Click Open to the dialog box that appears and a new Form2 will be generated in the main edit screen and will also appear in the Solution Explorer Window.

To make MyForm2 visible, for example on the click of a button, you need to add the following code Private Sub Button1_Click(. . My.Forms.Form2.Show() Me.Hide() End Sub .) Handles Button1.Click

Try running this program and when you click the button on Form1, Form2 will be displayed, and Form1 will be closed. Now try changing the code behind the button slightly to open the second form as a dialog box. Private Sub Button1_Click(. . .) Handles Button1.Click My.Forms.Form2.ShowDialog() End Sub How does this affect the behaviour of your program? When do you think you would use the ShowDialog function in preference to the Show function?

Page 3 of 4

Software Development (VB.NET)

How to share data between forms


You are already familiar with the concept of creating variables at the class level of a form if that variable needs to accessed by more than one subroutine of the program. The variable will be visible to all parts of that form, but NOT to any other forms in your program. If you are using multiple forms, you may need to share the same data. In order for the data entered in one form to be visible in another, the variable must be declared outside of both forms, and made visible to both. This is done by means of creating a module, and declaring the variables there as Public, and therefore accessible to all parts of your program. An example of this would be if you had one form in your program for adding data e.g. you may be registering new students on a course. Another form may be used to view the students details, and add their results. The students would need to be declared as a public array, or class in a module. You create a new module by selecting it from the Project menu, and then confirming your choice. It will now be visible in the Solution Explorer, and you can edit it by double clicking to open it.

Module Module1 Public products As New ArrayList Public item As String End Module In this example, two variables have been declared, an array and a string variable.

Page 4 of 4

You might also like