Visual Basic.NET 2008 > Working with Forms

Loading and Showing Forms - Visual Basic.NET 2008

<< Previous Page Next Page >>

Most practical applications are made up of multiple forms and dialog boxes, and one of the operations you'll have to perform with multiform applications is to load and manipulate forms from within other forms' code. For example, you might want to display a second form to prompt the user for data specific to an application. You must explicitly load the second form and read the information entered by the user when the auxiliary form is closed. Or you might want to maintain two forms open at once and let the user switch between them. A text editor and its Find & Replace dialog box is a typical example (e.g.:- Notepad, Wordpad).

You can access a form from within another form by its name. Let's say that your application has two forms, named Form1 and Form2, and that Form1 is the project's startup form. To show Form2 when an action takes place on Form1, call the Show method of the auxiliary form:

Form2.Show

This statement brings up Form2 and usually appears in a button's or menu item's Click event handler. To exchange information between two forms, use the techniques described in the "Controlling One Form from within Another," section later in this chapter.

The Show method opens a form in a modeless manner: The two forms are equal in stature on the desktop, and the user can switch between them. You can also display the second form in a modal manner, which means that users can't return to the form from which they invoked it without closing the second form. While a modal form is open, it remains on top of the desktop, and you can't move the focus to any other form of the same application (but you can switch to another application). To open a modal form, use the ShowDialog method:

Form2.ShowDialog

The modal form is, in effect, a dialog box like the Open File dialog box. You must first select a file on this form and click the Open button, or click the Cancel button to close the dialog box and return to the form from which the dialog box was invoked. This brings up the topic of forms and dialog boxes.

A dialog box is simply a modal form. When we display forms as dialog boxes, we change the border of the forms to the setting FixedDialog and invoke them with the ShowDialog method. Modeless forms are more difficult to program because the user may switch among them at any time. Moreover, the two forms that are open at once must interact with one another. When the user acts on one of the forms, it might necessitate some changes in the other, and you'll see shortly how this is done. If the two active forms don't need to interact, display one of them as a dialog box.

When you're finished with the second form, you can either close it by calling its Close method or hide it by calling its Hide method. The Close method closes the form, and its resources are returned to the system. The Hide method sets the form's Visible property to False; you can still access a hidden form's controls from within your code, but the user can't interact with it. Forms that are displayed often, such as the Find & Replace dialog box of a text-processing application, should be hidden — not closed. To the user, it makes no difference whether you hide or close a form. If you hide a form, however, the next time you bring it up with the Show method, its controls are in the state they were the last time.

The Startup Form

A typical application has more than a single form. When an application starts, the main form is loaded. You can control which form is initially loaded by setting the startup object in the project Properties window, shown in Figure 5.10. To open this dialog box, right-click the project's name in the Solution Explorer and select Properties. In the project's Properties pages, select the Application tab and select the appropriate item in the Startup Form combo box.

In the Project properties specifying the start up Form thjat's displayed when application starts

Figure 5.10 - In the Project properties specifying the start up Form thjat's displayed when application starts

By default, the IDE suggests the name of the first form it created, which is Form1. If you change the name of the form, Visual Basic will continue using the same form as the startup form with its new name.

You can also start an application by using a subroutine, without loading a form. This subroutine is the MyApplication Startup event handler, which is fired automatically when the application starts. To display the AuxiliaryForm object from within the Startup event handler, use the following statement:

Private Sub MyApplication Startup(...) Handles Me.Startup
System.Windows.Forms.Application.Run(New AuxiliaryForm())
End Sub

To view the MyApplication Startup event handler, click the View Application Events button at the bottom of the Application pane, as shown in Figure 5.10. This action will take you to the MyApplication code window, where you can select the MyApplication Events item in the object list and the Startup item in the events list.

Controlling One Form from within Another

Loading and displaying a formfromwithin another form's code is fairly trivial. In some situations, this is all the interaction you need between forms. Each form is designed to operate independently of the others, but they can communicate via public variables (see the following section). In most situations, however, you need to control one form from within another's code. Controlling the form means accessing its controls, and setting or reading values from within another form's code.

In Chapter "Windows Controls in VB.NET", you developed the Text Editor application, which is a basic text editor and consists of the main form and an auxiliary form for the Find & Replace operations. All other operations on the text are performed with menu commands on the main form.When the user wants to search for and/or replace a string, the program displays another form on which the user specifies the text to find, the type of search, and so on. When the user clicks one of the Find & Replace form's buttons, the corresponding code must access the text on the main form of the application and search for a word or replace a string with another. The Find & Replace dialog box not only interacts with the TextBox control on the main form, it also remains visible at all times while it's open, even if it doesn't have the focus, because its TopMost property was set to True.

Sharing Variables between Forms

The preferred method for two forms to communicate with each other is via public variables. These variables are declared in the form's declarations section, outside any procedure, with the keyword Public. If the following declarations appear in Form1, the variable NumPoints and the array DataValues can be accessed by any procedure in Form1, as well as fromwithin the code of any form belonging to the same project:

Public NumPoints As Integer
Public DataValues(100) As Double

To access a public variable declared in Form1 from within another form's code, you must prefix the variable's name by the name of the form, as in the following:

Form1.NumPoints = 99
Form1.DataValues(0) = 0.3395022

You can use the same notation to access the controls on another form. If Form1 contains the TextBox1 control, you can use the following statement to read its text:

Form1.TextBox1.Text

If a button on Form1 opens the auxiliary form Form2, you can set selected controls to specific values before showing the auxiliary form. The following statements should appear in a button's or menu item's Click event handler:

Form2.TextBox1.Text = "some text"
Form2.DateTimePicker1.Value = Today
Form2.Show()

You can also create a variable to represent another form and access the auxiliary form through this variable. Let's say you want to access the resources of Form2 from within the code of Form1. Declare the variable auxForm to represent Form2 and then access the resources of Form2 with the following statements:

Dim auxForm As Form2
auxForm.TextBox1.Text = "some text"
auxForm.DateTimePicker1.Value = Today
auxForm.Show

Multiple Instances of a Single Form

Note that the variable that represents an auxiliary form is declared without the New keyword. The auxForm variable represents an existing form. If we used the New keyword, we'd create a new instance of the corresponding form. This technique is used when we want to display multiple instances of the same form, as in an application that allows users to open multiple documents of the same type.

Let's say you're designing an image-processing application, or a simple text editor. Each new document should be opened in a separate window. Obviously, we can't design many identical forms and use them as needed. The solution is to design a single form and create new instances of it every time the user opens an existing document or creates a new one. These instances are independent of one another and they may interact with the main form. Usually they don't, because they aren't auxiliary forms; they contain the necessary interface elements, such as menus, for processing the specific document type, and users can arrange them any way they like on the desktop.

The approach described here is reminiscent of Multiple Document Interface (MDI) applications. The MDI interface requires that all windows be contained within a parent window and, although once very popular, it's going slowly out of style. The new interfaces open multiple independent windows on the desktop. Each window is an instance of a single form and it's declared with the New keyword. I've used this style of interface to redesign the TextPad application of Chapter 6, and I've included the revised application in this chapter's projects for your reference. Open the project in Visual Studio and examine its code, which contains a lot of comments.

<< Previous Page Next Page >>

Table of Contents

  1. Working with Forms in Visual Basic.NET
  2. VB Forms - Forms in Visual Basic.NET - The Appearance of Forms
    1. Properties of the Form Object in VB.NET
    2. Placing Controls on Forms in VB.NET
    3. Setting the TabIndex Property of the controls in a Form in Visual Basic.NET
    4. An Example Contacts Project using Forms in Visual Basic.NET
  3. Anchoring and Docking of Control in a Form in Visual Basic.NET
  4. Splitting Forms into Multiple Panes - Visual Basic.NET
  5. The Form’s Events - Visual basic.NET
  6. Loading and Showing Forms - Visual Basic.NET
    1. The Startup Form - Visual Basic.NET
    2. Controlling One Form from within Another - Visual Basic.NET
  7. Forms versus Dialog Boxes - Visual Basic.NET
    1. The MultipleForms Example - Forms in VB.NET 2008
  8. Building Dynamic Forms at Runtime - VB.NET 2008
    1. The Form’s Controls Collection - Visual Basic 2008
    2. Creating Event Handlers at Runtime - Forms in Visual Basic.NET
  9. Designing Menus, The Menu Editor in Visual Basic.NET
  10. The ToolStripMenuItem Properties - Visual Basic.NET
  11. Manipulating Menus at Runtime - VB.NET 2008
  12. Iterating a Menu’s Items - Vb.NET 2008

     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us | Privacy Policy