Professional Documents
Culture Documents
The user interface is the link between the user and the application. The usability of
the application depends on the interface. While creating an application in VB,
the form is the basic element of the user interface. Controls are then added to
provide specific functionality. Both these objects can be modified by the
programmer by changing their properties, methods and events.
Designing the interface
Aligning controls
• The names of controls and forms must satisfy the following criteria:
• It must begin with a letter.
• must contain only letters, numbers and underscore character( _ ).
• Punctuation characters and spaces are not allowed.
• must not be longer than 40 characters.
Which of the following are valid object names:
Name Valid / Invalid
Lbx12 Valid
@Rs Invalid
_lb1 Invalid
123_label Invalid
?1 Invalid
Star1 Valid
%age Invalid
We can use a prefix to describe a class, followed by a descriptive name
for the control. Using this naming convention makes the code more
self-descriptive and alphabetically groups similar objects in the
object list box.
Chekbox chk
Combobox Cbo
Hscrollbar Hsb
Timer Tmr
DirListBox Dir
Shape Shp
Control Prefix
Image Img
Line Lin
PictureBox Pic
TextBox Txt
CommandButton Cmd
OptionButton Opt
ListBox Lst
Properties
Properties are the object attributes. The controls / objects that we draw
on a form, have some properties associated with them. After a
control is added to a form, the programmer can set the
characteristics of the controls and the form such as size color, screen
location or the state of the control, such as enabled or disabled.
A few common properties are :
– Name
– Appearance
– Backcolor
– Forecolor
– Font
– Caption
– Width
– Height
– Left
– Top
– Enabled
– Visible
Working with Forms
In VB, the Form acts as the container for all the controls that make up
the interface. The form is the top-level object in a VB application,
and every application starts with the Form.
Characteristics of a form:
Control menu: On left end of the title bar is the control menu icon.
Clicking this icon opens the control menu. The control menu contains the
following commands:
1. Restore
2. Move
3. Size
4. Minimize
5. Maximize
6. Close
On the right side of the title bars are the three buttons:
1. Minimize
2. maximize
3. close buttons
Property Description
Borderstyle This property determines the border’s style for a Form and
also its appearance. It can take any of the following values:
Value Description
0-None No border for form. Form
cannot be resized. This
setting should be avoided.
Value Description
3- Fixed dialog For fixed dialog boxes
Font Produces a font dialog box in which we can set the text’s
font name, style and size
Icon Describes the icon graphic image displayed on the taskbar
when the user minimizes the form
Moveable Specifies whether the user can move the form at runtime.
(i) Not loaded : Form lives on the disk file and doesn’t take up any
resources ( such as memory etc.)
(ii) Loaded and hidden: Form is loaded into memory and is ready to
be displayed.
(iii) Loaded and shown : Form is shown and the user can interact with
it.
State of a form can be changed by using Load, Unload, show and
Hide methods.
Load <formname>
Unload <formname>
The load method does not display the form on the desktop, it just loads
the form in the memory.
The unload method removes a form from the memory and the
resources occupied by it are released so that they can be used by
other applications
Showing Forms
Syntax
Hiding forms
<formname>.hide
A hidden form remains in memory and can be displayed instantly with
show method.
Label Control
A label control displays text that user cannot directly change. The actual text
displayed in a label is controlled by its caption property.
Major Properties of Label control are:
2. Font – the property used to specify the display font, style and size for the
caption of the label
Textboxes are versatile controls that can be used to get input from the
user or to display text.
3. Font – the property used to specify the display font, style and size for the
caption of the label
4. Maxlength: property used to set the maximum number of characters
allowed in the textbox
Note : The amount of text that can be placed in a multiline Textbox control is
limited approximately to 32 KB. A single line textbox can hold only 255
characters.
5. Enabled : this property is used to set a value that determines if the text box
can respond to the events that are generated by the user
6. Multiline: this property, allows multiple lines of text in the text box.
10. Selstart: property that returns or sets the position of the first
character of the selected text.
11. SelLength: property that returns the length of the selected text.
Command button
Properties
Caption
This property holds the text that appears on the command button
BackColor
Cancel
If this property is set to True, pressing the Escape key at runtime will
activate the CommandButton's Click event. Only one
CommandButton on the Form can have this property set to True.
Default
This property determines if the command button responds to an Enter
keypress even if another control has the focus. The
CommandButton, which has this property set to True, will also
appear with a darker border.
Enabled
When this property is set to False, the user is not able to interact with
the CommandButton in any way e.g. it cannot be clicked on or have
focus. The caption will appear greyed out.
Font
In design time, this brings up the Font dialog box to allow us to choose
the font name, style, size and whether the Strikeout and Underline
attributes are set.
Height
Sets the height of the CommandButton.
Left
Sets the X coordinate of the CommandButton.
Top
Sets the Y coordinate of the CommandButton.
Visible
Determines whether the CommandButton appears on the Form. I.e. if
set to False, the CommandButton will be unavailable to the user -
even by trying to set focus to it with the tab key.
Width
Sets the width of the CommandButton.
Picture
Sets the picture, which is displayed in the background of the
CommandButton. The Style property needs to be set to 1 - Graphical
for this property to work.
Style
This needs to be set to 1 - Graphical if you want to use the graphical
properties of the CommandButton (apart from Fonts).
TabIndex
Sets the order in which the controls will get focus when the tab key is
pressed. The control that has 0 as its TabIndex will have focus when
the Form is loaded.
TabStop
If you set this to False, the CommandButton will not get focus when the
tab key is pressed.
Tag
This property can be set to a string if you need to store any extra
information related to the CommandButton.
ToolTipText
When this property is set, the text will popup over the CommandButton
when the user hovers the mouse pointer over it. This can be useful if
we cannot fit all the text you need to in the Caption.
Assigning access keys
For example, if we want to assign Alt + P as the access key for a print
command button, we can set the caption property to &Print. VB
shows this key by underlining it i.e. this caption appears as Print.
Frame control
Caption
Property to display text in the frame control
Font
Property to set font, style, and size of captioned text of frame
(Name)
property to name the frame
Visible
Property used to set a value indicating whether the frame should be visible
( value – True) or not (value – false)
Borderstyle
Property used to specify the style of border.
0- None
1- Fixed single
Appearance
Property used to set look of the frame
0 – Flat
1- 3D
OptionButton Control (Radio Button)
The option buttons are used to offer a small set of options from which a
user can choose one.
Caption
Property to display text for option button
Name
Property to name the option button
Value
Property to set the state of option button. If set to True, the option
button appears selected.
Alignment
Property to set the alignment of the option button.
0- Left justify – option button appears on the left of caption
Style
This property is used to set the appearance style which can either be 0-
standard (default) or 1- graphical ( it makes option button appear as a
command button.
Enabled
Property used to set a value that determines whether a checkbox can
respond to user-generated events
Checkbox control
The checkbox controls are used for offering a small set of choices from
which a user can choose one or more options.
Caption
Property the specify text to be displayed.
Name
Property to specify name
Alignment
Property to set the alignment of the checkbox.
0- Left justify – checkbox appears on the left of caption
Value
Property used to specify the state of checkbox. If its value is :
The listbox control presents a list of choices to the user. The choices
are presented in one or more columns. If the number of items
exceeds what can be displayed in a list box, scrollbars automatically
appear on the control.
Major properties of listbox control are :
Multiselect : This property determines whether the user can select the list’s
multiple items or not. This property must be set at design time. At runtime
we can’t change this property, we can only read its value.
Syntax
Syntax
<Listname>.Removeitem <index>
Clearing the List
<listname>.Clear
<Listname>.list(<index>)
<listname>.listcount
Combo Box
Value Description
A scrollbar is a long stripe with an indicator that lets the user select a value
between the two ends of a control.
The scrollbar comes in two forms :
The only difference between the two is their orientation. The left end ( in a
horizontal scroll bar) and the top end ( in a vertical scroll bar) corresponds
to its minimum value and the other end corresponds to its maximum value.
The current value of the scrollbar control is determined by the position
of the indicator ( or scrollbox) which is scrolled between the
minimum and the maximum values.
Event Description
Appearance 0 – Flat
1 – 3D . We can view the result of this property
if we set a border to the image control.
Borderstyle 0 – None
1 – Fixed Single
E.g.
Image1.Picture= Loadpicture(“c:\images\fountain.gif”)
To clear the graphics from the image control, we set the picture
property to Nothing;
Image1.Picture= Nothing
Image1.Picture=Loadpicture
Picturebox control
Appearance 0 – Flat
1 – 3D . We can view the result of this property
if we set a border to the picture box
control.
Borderstyle 0 – None
1 – Fixed Single
Pictures can be loaded into the picture box control at design time by
selecting the Picture property.
If we want to load or change picture at runtime, we can do this by
using the picture property and the Loadpicture function.
E.g.
picture 1.Picture=Loadpicture
Difference between Image box control and picture box control
Picture box control can be used to display graphics and can also be used as a
container control. It also provides support for graphics properties and
methods. Picture Box controls support all the properties related to graphic
output, including AutoRedraw, ClipControls, HasDC, FontTransparent,
CurrentX, CurrentY, and all the Drawxxxx, Fillxxxx, and Scalexxxx
properties. PictureBox controls also support all graphic methods, such as
Cls, PSet, Point, Line, and Circle and conversion methods, such as ScaleX,
ScaleY, TextWidth, and TextHeight.
The sizing behavior of the image control differs from that of picture box
control. It has the Stretch property while the picture box has as
Autosize property.
Stretch property of Image box
this property determines whether the image is resized if the size of the
imagebox control is different from the picture loaded into it.
False Only the part of the image that can fit in the control is displayed
The image box control is good for displaying graphics and uses fewer
resources than picturebox controls. The picture box control provides
methods for drawing at runtime and is much more flexible than
image box control.
Timer Control
• The actions that you want to happen at the specified interval are
coded in the Timer's Timer event.
• We specify the interval at which we want actions to occur, in the
Timer control's Interval property. The value is specified in
milliseconds (1000 milliseconds = 1 second). The timer control
triggers a timer event after each interval of time goes by.
The interval property contains a value that must range from 1to 65535.
• We turn the timer "on" or "off" by setting its Enabled property to
True or False, respectively. The Enabled property is set to True by
default. Also setting the interval property to 0 disables the timer
control.
Program for Blinking text
The Timer control can be used to make an object on your screen appear
to blink. This is done by toggling the Visible property of the object
on and off at short intervals. The Visible property is available to
nearly all controls. It is a Boolean property that determines whether
or not the control is visible to the user at run time by setting its value
to True or False To build a simple demo, perform the following
steps.
Start a new VB project.
Place a label on the form and name it lblProcess. Set its Caption to
Processing . . ..
Set it to a large font and set its Forecolor property to the color of your
choice. Center it on your form.
Place a timer on the form and name it tmrBlink. Set its Interval
property to 250.
In the tmrBlink_Timer event, place the following single line of code:
lblProcess.Visible = Not lblProcess.Visible
Run the project. The label will blink