|
Working with Forms
Adding a New Form to the Project
You can add a new form to the project with which you are working. To do that,
select File->Add New Item from the main menu which
displays a window asking you what to add to the project. Select Windows
Form from the window and click Open to add a new form to the project. Alternatively,
you can add a new form with the Solution Explorer. To add a new form with the Solution
Explorer, right-click on the project name in Solution Explorer and Select Add->Add
Windows Form. Once you finish adding a new form, if you want the form which
you added to be displayed when you run the application you need to make some
changes. You need to set the new form as Startup Object.
To do that, right-click on the project name in Solution Explorer window and
select Properties which displays the Property Pages window.
On this window click the drop-down box which is labeled as Startup Object. Doing that
displays all the forms available in the project. It looks like the image below.
Select the form which you want to be displayed when you run the application and
click Apply. Now, when you run the application, the form you assigned as Startup
object will be displayed.
Working with Multiple Forms
Let's see how we can work with more than one form/open a new form. Say, for example,
we want to open a new form (Form2) when a button in the current form (Form1) is clicked.
To do that, open a new project by selecting File->New->Project->Visual Basic->WindowsApplication.
This adds a form (Form1) to the application. Add a new form (Form2) by selecting
File->Add New Item->Windows Form. Also, drag a button (Button1) onto Form1.
We want to open Form2 when the button in Form1 is clicked. Unlike earlier versions
of Visual Basic, VB .NET requires us to refer to Form2 in Form1 in order to open it
i.e creating an object of Form2 in Form1. The code for that looks like this:
Public Class Form1 Inherits System.Windows.Forms.Form
Dim other As New Form2()
'Creating a reference to Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As_
System.EventArgs) Handles Button1.Click
other.Show()
End Sub
|
Visual Inheritance with Forms
As we know, Inheritance allows us to derive one class from another. VB. NET allows
us to inherit one form from another. Let's see how we can inherit a new form
from an existing form. Say, we have a form named Form1 with some controls on it and
we want to inherit a new form from that form. To derive a new form from the existing
one select Project->Add Inherited Form from the main
menu. Doing that opens the Add New Item window. Double-click on the Inherited
Form Icon in the templates box to open the Inheritance Picker.
Inheritance Picker window looks like the image below.
Select Form1 in the picker and click OK. Clicking OK adds a new form, Form2, which
is derived from Form1. Everything on Form2 including the title is copied from Form1. The
only difference between form1 and form2 is, the controls on Form2 come with a special
icon at the upper left corner which indicates that the form is inherited and the controls
are locked. The image below displays a Inherited Form.
Inheriting a Form from Other Project
You can also inherit a form from other project. To inherit a form from other project,
navigate to the project containing the form you want using the browse button in the
Inheritance Picker dialog, click the name of the DLL file containing the form and
click Open. This returns to the inheritance Picker dialog box where the selected project
is now listed. Choose the appropriate form and click OK. A new inherited form is added
to your project.
|