Overview of Parent Controls |
|
|
An application's object is programmatically referred to
as a parent when it can host, hold, or carry other objects. For this reason,
such a window is also referred to as a container. Although the first object
that comes in mind is the form, this is not the only window that can act as
parent. This means that, in your application programming adventure, you will
get acquainted with various types of containers and they are meant to play
different roles.
|
In some cases, two or more containers can be used to
implement the same functionality. In some other cases, your choice will be
narrowed based on your goal.
The most common and the most widely used container is
the form. In an application, a form can be configured to display like a
regular dialog box, to become a Single Document Interface (SDI) or to
participate in a Multiple Document Interface (MDI) application.
As mentioned already, there are two categories of
controls: parents and children:
- Parent: The child controls are positioned on a parent. To keep
track of its children, a parent control has the Controls
property. To create a control inside of a parent, a child control is
added to the Controls property of the parent using the
Controls.Add() method. When a parent control is created, it "gives
life" to other windows that can depend on it. When a parent is
destroyed, it also destroys its children. A parent control "carries",
"holds", or hosts the controls positioned on it. When a parent is
created, made active, or made visible, it gives existence and
visibility to its controls. When a parent gets hidden, it also hides
its controls. If a parent moves, it moves with its children. The
controls keep their positions and dimensions inside the parent. The
.NET Framework provides various parent controls.
- Child: A window is referred to as child when its existence, its
visibility, and its availability depend on another window referred to
as its parent. Except for the forms, all of the Windows controls you
will use in your applications are child controls and they must be
parented by another control.
A child window can be a parent of another control. For
example, the Standard toolbar of Visual Studio 2005 is the parent of the
buttons on it. If you close or hide the toolbar, its children disappear.
At the same time, the toolbar is a child of the application's form. If you
close the application, the toolbar disappears, along with its own
children. In this example, the toolbar is a child of the form but is a
parent to its buttons.
The type of container you want to use dictates how you
acquire that container and add it to your application. Parent controls are
somewhat divided in two broad categories: primary parents and intermediate
parents. Once you have spent time with them, you will decide which one and
when to use a particular control.
The primary type of control parenting you will use is
a form. This is used as the platform for other controls, including other
containers. Therefore, when you start your application, you first decide
on the type of application. If you create a Windows Forms application, it
gets automatically equipped with a form on which you can add child
controls. When the application executes, it can present its contents to
the user:
The second category of parents you will encounter
qualify as intermediate. Theses containers cannot be the base of an
application as does the form. These parents must be positioned on another
parent first, then they can host their own children. An example of such a
parent is the property page also called tab control. This control must be
hosted by a form or dialog box. Here is an example of a property sheet
(dialog box) that hosts three property pages (tab controls) and each
property page hosts its own child controls:
Another type of intermediary container is the toolbar
that is usually used to host various buttons.
Introduction to Dialog Boxes
|
|
A dialog box is a form defined with particular
properties. Like a form, a dialog box is referred to as a container. Like
a form, a dialog box is mostly used to host child controls, insuring the
role of dialog between the user and the machine. Here is an example of a
dialog box:
A dialog box has the following characteristics:
- The only system button it is equipped with is Close
.
As the only system button, this button allows the user to dismiss the
dialog and ignore whatever the user would have done on the dialog box
- It cannot be minimized, maximized, or restored. A dialog box does
not have any other system button but Close
- It is usually modal, in which case the user is not allowed to
continue any other operation on the same application until the dialog
box is dismissed
- It provides a way for the user to close or dismiss it
Practical
Learning: Introducing Dialog Boxes
|
|
- Start Microsoft Visual Basic and create a new Windows Application
named SolasPropertyRental1
- From the Common Controls section of the Toolbox, click ListView
and click the form
- While the list view is still selected, in the Properties window,
change the following characteristics
(Name): LvwProperties
View: Details
- Still in the Properties window, click Columns and click its
ellipsis button
- In the ColumnHeader Collection Editor, click Add
- In the right list, click Text and type Property #
- Click Add.
In the right list, click Text and type Property
Type
- Click Add.
In the right list, click Text and type Bedrooms
- Click Add.
In the right list, click Text and type Bathrooms
- Click Add.
In the right list, click Text and type Monthly
Rent
- Click OK
- Complete the design of the form as follows:
|
Control |
Text |
Name |
ListView |
|
|
Button |
New Property... |
BtnNewProperty |
Button |
Close |
BtnClose |
|
- To add another form to the project, on the main menu, click
Project -> Add Windows Form...
- In the middle list, make sure Windows Form is selected.
Set the
Name to PropertyEditor and click Add
To create a dialog box, you start with a form, which
you can get by creating a Windows Application or deriving a class from
Form. Here is an example:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
Text = "Domain Configuration"
Width = 320
Height = 150
Location = New Point(140, 100)
StartPosition = FormStartPosition.CenterScreen
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
Characteristics of Dialog Boxes
|
|
The Border Style of a Dialog Box
|
|
There are a few actions you should perform on a form
to transform it into a dialog box; but normally, these are only
suggestions, not rules. Based on the Microsoft Windows design and
standards, to create a dialog box, you should set a form's
FormBorderStyle property to FixedDialog. Setting this property
changes the borders of the form to the standard borders of a dialog box
(the border of a dialog box is thinner than that of a regular form). You
can set this characteristic in the Properties window or programmatically.
Here is an example:
Public Sub InitializeComponent()
Text = "Domain Configuration"
Width = 320
Height = 150
Location = New Point(140, 100)
StartPosition = FormStartPosition.CenterScreen
FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
End Sub
The Minimize and Maximize Boxes
|
|
Besides taking care of the border, you should also set
both the MinimizeBox and the MaximizeBox properties to
False. This causes the window to display only the system Close button.
You can set these characteristics in the Properties window or
programmatically. Here is an example:
Public Sub InitializeComponent()
Text = "Domain Configuration"
Width = 320
Height = 150
Location = New Point(140, 100)
StartPosition = FormStartPosition.CenterScreen
FormBorderStyle = Windows.Forms.FormBorderStyle.FixedDialog
MinimizeBox = False
MaximizeBox = False
End Sub
This would produce:
Practical
Learning: Configuring a Dialog Box
|
|
- To transform the PropertyEditor form into a dialog box, in the
Properties window, change its characteristics as follows:
FormBorderStyle: FixedDialog
StartPosition: CenterParent
MinimizeBox: False
MaximizeBox: False
ShowInTaskbar: False
- Design the dialog box as follows:
|
Control |
Text |
Name |
Label |
Property Type: |
|
TextBox |
|
TxtPropertyType |
Button |
Cancel |
BtnCancel |
Label |
Bedrooms: |
|
TextBox |
|
TxtBedrooms |
Button |
OK |
BtnOK |
Label |
Bathrooms: |
|
TextBox |
|
TxtBatrooms |
Label |
Monthly Rent: |
|
TextBox |
|
TxtMonthlyRent |
|
You should provide a way for the user to close the
dialog box. A dialog box should have at least one button labeled OK. This
button allows the user to acknowledge the message of the dialog box and
close it by clicking the button. If the user press Enter, the dialog box
should also be closed as if the OK button was clicked.
Often the user will be presented with various options
on a dialog box and may be asked to make a decision on the available
controls. Most of the time, if you are creating such a dialog box, besides
the OK button, it should also have a Cancel button. The OK button should
be the default so that if the user presses Enter, the dialog box would be
closed as if the user had clicked OK. Clicking OK or pressing Enter would
indicate that, if the user had made changes on the controls of the dialog
box, those changes would be acknowledged and kept when the dialog box is
closed and usually the changed values of the control would be transferred
to another dialog box or form. Keep in mind that you are responsible for
implementing this functionality.
To fulfill this functionality of the OK button, after
adding it to a dialog box (or form), open the AcceptButton combo
box in the Properties window for the form and select the name of the
button.
Practical
Learning: Accepting an Action
|
|
- Click an unoccupied area of the form
- In the Properties window, click the arrow of the AcceptButton
field and select BtnOK
The Cancel button is used to allow the user to dismiss
whatever changes would have been made on the controls of the dialog box.
The dialog box should also be configured so that if the user presses Esc,
the dialog box would be closed as if the user had clicked Cancel.
To fulfill this functionality of the Cancel button,
after adding it to a dialog box (or form), open the CancelButton
combo box in the Properties window for the form and select the name of the
button.
Besides the OK and the Cancel buttons, a dialog box
can be created with additional buttons such as Finish or Help, etc. It
depends on its role and the decision is made by the application developer.