Lecture 03
Lecture 03
Session Objectives In most languages, we are taught to read from left to right, top
VISUAL BASIC
After this session you will be able to to bottom of a page. The same holds true for a computer
• Design a form customize it properties, access the methods screen -most users’ eyes will be drawn to the upper left portion
and understand events of the screen first, so the most important element should go
there.
• Identify the controls for Displaying and Entering Text
For example, if the information on a form is related to a
• Set the properties of the controls
customer, the customer’s name should be right portion of the
• Access the methods screen; the user normally will not access these until he has
• Trigger the events finished working with the form
Designing a Form Logically related information or controls can be grouped for
In the previous chaper we learned about the Visual Basic better navigation through the form. Frame controls should be
environment. In this chapter you will learn about the Visual used to help reinforce the relationships between controls:
Basic form which is displayed as the window ie the interface of Uniformity of Interface Elements
an application. We will also learn to use label, text box and other A uniform look gives a feeling that everything fits together. A
control which can be placed on the form. We use these controls lack of uniformity in an interface can be confusing, and can
to display data and accept input from the user. make an application seem disorganized, possibly even cause the
Form objects are the basic building blocks of a Visual Basic user to doubt the reliability of an application
application, the actual windows with which a user interacts Elements such as the types of controls, standard to size and
when they run the application. Form is the main part of a user grouping of controls, and font choices should be designed in
interface. It is through forms and their objects that a user advance. Though Visual Basic provides a wide variety of
interacts with the application. Forms have their own properties, controls, the ones that best fit the application should be chosen.
events, and methods with which you can control their appear-
For example, a combo box can be used to save space on a form.
ance and behavior. Every time Visual Basic is started, a new
Because the full list is not displayed until the user clicks the
default Form1 is added to the environment. This form has a
down arrow. It can easily fit in a small space where a list box
default set of properties that can be changed.
would not fit. A list box is appropriate when we want to reduce
Good User Interface Design the amount of typing required of the user.
The user interface is perhaps the most important part of an Also, if a text box control is to be used to display text, a label
application; it’s certainly the most visible. To users, the interface control is usually more appropriate for that purpose. Similarly,
is the application; as they are not aware of the code that is if a user has to select a single option given multiple options an
executing behind the scenes. The usability of an application option button would do the needful.
depends on the interface.
The setting of properties for all the controls is equally impor-
Although Visual Basic makes it easy to create user interface by tant to maintain uniformity. For example, if one label displays
simply dragging controls onto a form, a little planning up front text in italics another should not display it in bold unless there’s
can make a big difference in the usability of our application a good reason. A style is to be selected and stuck to throughout
Before starting out it would be a good practice to draw the form the application, even if it means redesigning some features.
designs on paper first, determining which controls are needed, Too many controls on a form can, lead to a cluttered interface,
the relative importance of the different elements, and the making it difficult to find an individual field or control. An
relationship between controls orderly interface makes it easy for the user to read and under-
The layout of the form not only influences its aesthetic appeal, stand the interface. Uniform spacing between controls can make
it also has a tremendous impact on the usability of the the design more usable
application. Layout includes such factors as positioning of Figure Forms and controls have their own properties,
controls, uniformity of elements, and simplicity of design. events, and methods
Positioning of Controls
In most interface designs, not all elements are equal importance.
Careful designing is necessary to ensure that the more
important elements are readily apparent to the user. Important
or frequently accessed elements should be given a position of
prominence; less important elements should be assigned less
prominent locations.
VISUAL BASIC
The appearance of the form’s’ border is controlled by the
BorderSty/e property. This also decides whether a form can be
resized or not. This property can only be set at design time
able 1.5 lists the BorderSty/e property settings for a form
object.
and controls, set their properties, and write code for their events Minimize buttons
Is resizable using any of the optional border elements listed for
at design time, which is any time you’re building an application VbSizable 2
setting 1 (Default)
in the Visual Basic environment. Run time is any time you are
Includes the Control menu box and title bar; cannot include
actually running the application and interacting with the VbFixedDialog 3
Maximize or Minimize buttons and is not resizable
application as the user would. Displays a non-sizable window with a Close button and title
The best way to familiarize yourself with the many form VbFixedToofWindow 4 bar text in a reduced font size. The form does not appear in
the Windows 95 taskbar
properties is to experiment. Change some of the properties of
Displays a sizable window with a Close button and title bar
a form in the Properties window (Figure), then run the VbSizableTooIWindow 5 text in a reduced font size. The form does not appear in the
application to see their effect. You can learn more about each Windows 95 taskbar
property by selecting it and pressing F1 to view the context-
sensitive Help.
Figure The Properties window Control Box
The Contra/Box property determines whether a standard
Windows control box can be displayed on a form. When the
property is set to True, the BorderStyle property must also be
set to either 1, 2 or 3 (vbFixedSingle, vbSizable, or
vbFixedDouble) to display the control box.
MaxButton and MinButton
The MaxButton and MinButton properties determines whether
standard Windwows Maximize and Minimize buttons are
displayed in the form’s title bar. For these properties to work,
the BorderStyie property has to be set to 1, 2 or 3
(vbfixedsingle, vbsizable, or vbFixedDialog).
Name
By default, the name for a new form is Form followed by a
unique integer. The first Form object is Form1 the second is
Form2, and so on.
A form’s name is used to reference it in code. Therefore, it is
important to set the Name property when the form is created.
Setting form Properties The name must start with a letter, and can include numbers and
Many of a form’s properties affect its physical appearance. underscore characters but it cannot include spaces or punctua-
Forms have properties that determine aspects of their tion. It’s a good idea to set the Name property to something
appearance such as position, size color and aspects of their more meaningful, such as “frmEntry” for an order entry form.
behavior such as whether or not they are resizable. The Icon
properties of a form can be set or changed either at design time The Icon property specifies the icon that appears when a form is
or at run time. We shall discuss a few of the properties in the minimized. In Windows 95, the icon also
following sections.
appears in the title bar
Caption The Icon property is set at design time. Visual Basic has a large
Caption property determines the text that is displayed in the library of icons for applications
form’s title bar. When the form is minimized, the caption is
seen below the icon. To set the Caption property at design time, Font
you need to use the Properties window. The form’s Font property is used to set the font to use on the
form. By default, other controls placed on the form use the font
set for the form
The WindowState property determines how the form will To provide this feature Use this control
appear when displayed (normal, minimized, or maximized). Text that can be edited by the user, for example an order
Text box
The property is set at run time. Before a form is displayed, the entry field or a password box
WindowState property is always set to vbNormal, regardless of Text that is displayed only, for example to identify a field Txte
Label
its initial setting on a form or display instructions to the user
VISUAL BASIC
Check Your Progress
point within the string of text, with 0 being the left-most
position.
• What is the purpose of the Name property of a control
• The SelLength property is a numeric value that sets the width
• What is the purpose of the caption property of a form
of the insertion point. Setting the SelLength to a number
greater than 0 causes that number of characters to be selected • State and explain the important methods of a form
and highlighted, starting from the current insertion point. • State and explain the the sequence of events trigged for a
• If the user starts typing while a block of text is selected, the form
selected text will be replaced. In some cases, you might want • You can display program output in a text box or a label.
to replace a text selection with new text by using a paste When should you use a text box. When is a label
command. The SelText property is a string of text that you appropriate.
can assign at run time to replace the current selection. If no • What is the use of PasswordChar property
text is selected, SelText will insert its text at the current
• Which are the properties that is used to edit the text in a text
insertion point.
box
• MaxLength: For text input, you can put a limit on the
number of characters the user can type into the text box with Notes
this property. For instance, if you created a text box where
the user had to enter in his password and the password
would always be 10 characters or less, you could set the
.MaxLength property of the text box to 10. When the user
tries to type the 11th character, he will recieve a beep.
Multiline: If the .Multiline property is set to true, then
when the user presses enter the cursor will go to the next
line, like a word processor. Be sure you make the text box
high enough for the user to type several lines if you don’t
plan to make a Vertical scroll bar.
• PasswordChar: If you’ve ever seen a password input box,
you’ll notice that the password the user types isn’t printed in
regular characters for the whole world to see; rather, asteriks
are displayed for each character the user types. If you are
doing a password input box, the .PasswordChar property
can be usefull since it sets what character appears when the
user types. You would usually want to set this to *, but
anything else can be used as well.
• ScrollBars: There are 4 possible values which can be set for
this control:
0 - None
1 - Horizontal
2 - Vertical
4 - Both
If you have the .Multiline property set to true, you will usually
want the ScrollBars property set to 2 (vertical). When one of
these is set, scroll bars appear on the text box so the user can
control where the visual window is. If a Horizontal scroll bar is
enabled with the text box, then the user will have to push enter
to advance to the next line since it will continue scrolling to the
right without change.