Multiple Forms, Cont..

advertisement
Multiple Forms, Container
Controls, AddHandler
This presentation is based on the
Forms and ContainerControls VB
Projects
1
Using Multiple Forms
• Extremely powerful and complex programs can be written
using just one form.
• However, in many cases additional forms will be useful in
providing the functionality you need for a successful
program.
• Many programs, like photo-editing applications (and Visual
Studio) use lots of subordinate windows: windows which
provide tools to assist in the work of the main form (the
Toolbox is the most obvious example).
• Other programs, such as Microsoft Flight Simulator, use
multiple windows that are more or less equal: a front view,
side view, instrument panel, and so on.
• In both cases, being able to have multiple windows (forms)
in an application makes it more useful and powerful.
2
Each Form is a Class
• A Form is a Class, and as such each form in
your program can be considered as a subprogram.
• Each form has its own user interface, both
with the user and with other forms.
• Each form has its own code.
• As a Class, you can add properties,
constructors, and all of the other features of a
Class to any form you design.
3
Adding a Form
• From the Project menu, select Add Windows
Form. In the dialog box which appears, give
the form a descriptive name, like
“frmDisplay.vb”, and click the Add button.
• Your new form will appear in the Solution
Explorer window and in the form designer
window. You can now set its properties and
add controls to it.
4
Startup Form
• Once you have more than
one form in a project, you
can choose which one is
displayed when the
program starts.
• On the Project menu, the
bottom item is your
project’s properties. The
properties are organized
into several tabs along the
left.
• On the Application tab,
there is a Combo Box to
choose a startup form; pick
the one that you want.
5
Opening a Form at Runtime
• VB allows two ways to open a form.
• You can open the default instance of the form.
If your form is called Form1, you open the
default instance with this line of code:
• Form1.Show()
• For simple programs, this is good enough.
However, if you want to open multiple
instances of the form, the following approach
is preferred:
6
The Form is an Object
• Once you’ve designed Form1, you can declare
variables of that type.
• Because it is a Class, and not a primitive data
type like Integer or String, you must use
“New” to make an actual Form1 object.
• After the “New” statement, you can access
the form in code—call its procedures, set its
properties, etc.
• The form’s Show procedure makes it visible.
7
Show vs. ShowDialog
• There are two ways to display a new form: Show and
ShowDialog.
• Show displays the form "modelessly." That is, the form does
not take over your program. You can select it or deselect it
at will.
• ShowDialog opens the form "modally," which means that it
takes over control of your program, actually stopping code
execution at the line of code with the ShowDialog call in it.
At this point, you can only interact with the new form.
• ShowDialog is generally used for dialog boxes (hence the
name), where user input is required before proceeding
with the program. Control returns to the main part of the
program when the user clicks the OK button or the Cancel
button, or otherwise closes the form.
8
Container Controls
• The VB Toolbox contains several types of container
controls:
• Panel: General purpose for grouping controls
• FlowLayoutPanel: Allows controls of different sizes and
shapes to be arranged in order. Good for pictures.
• SplitContainer: Allows for objects to be divided into
two parts. Works well with drag-and-drop.
• GroupBox: Similar to panel, but has a caption. Usually
used with RadioButtons.
• TabControl: Creates a series of tabs, or pages, to
organize lots of controls into the confined space of a
form. Frequently used for wizards.
9
10
11
AddHandler
• When you add a control at design time, you
can assign an event handler to it in a few of
ways:
– You can double-click on the control.
– You can select the control, click on the lightning
bolt in the properties window, and then click the
event you want to respond to.
– You can pick the control from the left combo box
at the top of the code window, and then the event
from the right combo box.
12
AddHandler
• If you add a control at runtime, you only have
one option: AddHandler.
• With AddHandler, you assign a subroutine to
handle a particular event of a control.
• The subroutine must have the proper
parameters to handle that type of event.
13
Procedure for AddHandler
• Prepare the Sub that you will want your runtimeadded control to run. To guarantee that it has the
proper parameters, create the same type of
control at design time, and pick the event that
you will be handling.
• For example, if you are adding a button and want
to respond to its Click event, temporarily put a
button on the form at design time, and doubleclick on it to create a click event handler.
14
Procedure for AddHandler (cont.)
• Rename this new sub to match what it will do.
You can also remove the Handles part at the end
of the declaration.
• At this point, you can remove the button that you
added temporarily.
• In the code where you create your new button,
add the AddHandler line to connect the click
event to the new Sub.
• You can write the code for that sub at any time.
15
AddHandler Example
16
Download