Cit2457 VB Unit 4
Cit2457 VB Unit 4
Control: A control is a tool you use to create objects on a Visual Basic form. We can select
controls from the toolbox and use the mouse to draw objects on a form. Most of the controls
to create user interface elements, such as command buttons, image boxes, and list boxes.
Object : An object is a type of user interface element you create on a Visual Basic form by
using a toolbox control. (In fact, in Visual Basic, the form itself is also an object.) We can
move, resize, and customize objects by setting object properties and customize Visual Basic
objects by using event procedures that are fine-tuned for different conditions in a program.
LABEL
Label, the simplest control in the Visual Basic
toolbox, displays formatted text on a user interface
form. It is used to display static text, titles and
screen output from operations.
Typical uses for the Label control include:
Help text
Program splash screen headings
Formatted output, such as names, times, and
dates
Descriptive labels for other objects, including
text boxes and list boxes.
A label is a control use to display text that a user can't
edit directly. The text of a label box can be changed at
run-time in response to events.
Label Properties:
Alignment - Aligns caption within border.
Appearance - Selects 3-D or flat appearance.
AutoSize - If True, the label is resized to fit the text specifed by the caption
property. If False, the label will remain the size defined at design time and
the text may be clipped.
BorderStyle - Determines type of border.
Caption - String to be displayed in box.
Font - Sets font type, style, size
LABEL EVENTS
Click - Event triggered when user clicks on a label.
DblClick - Event triggered when user double-clicks on a label.
TEXTBOX
The text box is the standard control for accepting input from the user as well as to display
the output. It can handle string (text) and numeric data but not images or pictures. Strings in
a text box can be converted to a numeric data by using the function Val(text). The data typed
in is in the Text property of the control.
COMMAND BUTTON
A Command Button control is used to create buttons with a variety of uses on a form. It
displays an illusion that the button is pressed when the user click on it. The Command button
is the most basic way to get user input while a program is running. By clicking a command
button, the user requests that a specific action be taken in the program.
OPTION BUTTON
Option buttons, also called radio buttons, are typically used in a group of two or more. At
any one time, only one button in the group can be "on". Clicking an option button turns it
"on" and turns all other buttons in the group "off".
The Option Button control satisfies these requirements.
It displays a list of choices on a form and requires the user to pick one (only one) item
from a list of choices.
It returns true (1) or false (0) value.
CHECKBOX
The CheckBox control is similar to the option button, except that a list of choices can be
made using check boxes where you cannot choose more than one selection using an
OptionButton. By ticking the CheckBox the value is set to True.
Properties
If you want to display the control in a checked state, we set its Value property to 1-Checked
right
in the Properties window, we set a grayed state with 2-Grayed and value = 0 for Unchecked
Events
The only important event for CheckBox controls is the Click event, which fires
when either the user or the code changes the state of the control.
You usually write code in a CheckBox control's Click event when it affects the state
of other controls.
For example, the program will change the background color of the form to red when the
check
box is unchecked and it will change to yellow when the check box is checked.
LIST BOX
When you want a user to choose a single response from a long list of choices, you use the
ListBox control. (Scroll bars appear if the list is longer than the list box.)
.
Adding items to a List
It is possible to populate (fill) the list at design time or run time
Event Procedures
Change Called when text in ListBox is changed
DropDown Called when the ListBox drop-down list is displayed
GotFocus Called when ListBox receives the focus
LostFocus Called when ListBox loses it focus
COMBOBOX
A ComboBox combines the features of a TextBox and a ListBox. This enables the user to
select either by typing text into the ComboBox or by selecting an item from the list.
The Simple Combo box displays an edit area with an attached list box always visible
immediately below the edit area. A simple combo box displays the contents of its list all the
time. The user can select an item from the list or type an item in the edit box portion of the
combo box. A scroll bar is displayed beside the list if there are too many items to be
displayed in the list box area.
The Dropdown Combo box first appears as only an edit area with a down arrow button at
the right. The list portion stays hidden until the user clicks the down-arrow button to drop
down the list portion. The user can either select a value from the list or type a value in the
edit area.
The Dropdown list combo box turns the combo box into a Dropdown list box. At run time ,
the control looks like the Dropdown combo box. The user could click the down arrow to view
the list. The difference between Dropdown combo & Dropdown list combo is that the edit
area in the Dropdown list combo is disabled. The user can only select an item and cannot type
anything in the edit area.
Private VScroll1_Scroll()
Label1.Caption = VScroll1.Value
End Sub
TIMER CONTROL
The Timer control allows you to perform a task at a specified interval or to wait for a
specified length of time.
To execute a group of statements for a specified period of time, you can use the Timer
control in the Visual Basic toolbox.
The Timer control is an invisible stopwatch that gives your programs access to the
system clock.
You can use the Timer to:
Count down from a preset time.
Delay a program.
Repeat an action at prescribed intervals.
Objects that you create with the Visual Basic Timer:
Are accurate to 1 millisecond (1/1000 of a second).
Aren’t visible at run time.
Example : Create a digital clock using a timer and a simple label object
PICTURE BOX
The Picture Box controls is used to handle graphics.
You can load a picture during the designing phase by clicking on
the picture property in the properties window and select the
picture from the selected folder.
You can also load the picture at runtime using the LoadPicture
method.
The image in the picturebox is not resizable.
For example,
Picture1.Picture=LoadPicture ("D:\VB\Images\amc.jpg")
IMAGE BOX
The Image Box is another control that handles images and pictures. It functions almost
identically to the picture box.
One major difference between picture and Image control, the image in an Image Box is
stretchable, which means it can be resized. This feature is not available in the Picture Box.
For example
Image1.Picture=LoadPicture ("D:\VB\Images\amc.jpg")
LINE CONTROL
The Line control is a decorative control whose only purpose is let you draw one or more
straight lines at design time, instead of displaying them using a Line graphical method at
run time.
Properties:
BorderColor (the color of the line),
BorderStyle (the same as a form's DrawStyle property),
BorderWidth (the same as a form's DrawWidth property),
Line method at run time is usually better in terms of performance.
SHAPE CONTROL
The Shape control is an extension of the Line control. Shape property is important for this
control. Six basic shapes Rectangle, Square, Oval, Circle, Rounded Rectangle, and
Rounded Square are listed in Shape control..
It supports all the Line control's properties and a few more: BorderStyle (0-Transparent, 1-
Solid), FillColor, and FillStyle (the same as a form's properties with the same names).
Coding for Font Dialog box