100% found this document useful (1 vote)
379 views143 pages

Visual Programming - PDF

Visual Basic is an event-driven programming language. It has three editions: Standard, Professional, and Enterprise. The main features of Visual Basic include creating graphical user interfaces, supporting client/server architecture and COM/ODBC, and providing IntelliSense features like quick info, data tips, and listing members and constants. Forms are containers that hold controls to build the user interface. Form properties like name, appearance, background color, and visibility can be set. Events like load, click, and resize occur when interacting with forms. Methods like show, hide, and unload control how forms are displayed.

Uploaded by

Sindhu. S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
100% found this document useful (1 vote)
379 views143 pages

Visual Programming - PDF

Visual Basic is an event-driven programming language. It has three editions: Standard, Professional, and Enterprise. The main features of Visual Basic include creating graphical user interfaces, supporting client/server architecture and COM/ODBC, and providing IntelliSense features like quick info, data tips, and listing members and constants. Forms are containers that hold controls to build the user interface. Form properties like name, appearance, background color, and visibility can be set. Events like load, click, and resize occur when interacting with forms. Methods like show, hide, and unload control how forms are displayed.

Uploaded by

Sindhu. S
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 143

Introduction

➢ Visual Basic is a powerful application development tool. It is a Event-driven


Programming language.

➢ The name Visual Basic many be interpreted as follows:


Visual – Refers to the methods used to create the Graphical User
Interface (GUI) by using pre-built objects provided by VB.
Basic - Refers to the BASIC language used its basic syntax of
statements.(some statements, functions etc., which are related
directly to windows GUI).

➢ Visual Basic comes in three editions:

1. Standard
2. Professional
3. Enterprise
Features of Visual Basic
The main features of Visual Basic are as follows:

1. Visual basic is a graphical user interface, used to create interactive


application.
2. It supports client/server architecture, COM (Component Object Model)
and ODBC (Open Data-Base Connectivity).
3. . Microsoft has introduced a new feature called IntelliSense ( which
enables the system to interact with the user during coding)...

The five Intellisense features are:


a. Quick info
b. Complete word
c. Data Tips
d. List members
e. List Constants
4 Multiple projects can be involved in the IDE at once, which saves time for
coding and debugging.
5 Property window can be made to list the properties alphabetically or by
category.
6 Controls can easily be aligned and formatted.
7 Data Grid control.
8 Date time picker.
9 Data report.
10 IIS Application.
Start the Visual Basic by using the following steps

Click on start Programs Microsoft Visual Studio 6.0 Microsoft Visual Basic 6.0
Standard E X E

➢ It is a typical application and is used to create a standard executable file.


most of the examples in this book are standard EXE projects.
➢ On loading Visual Basic, the New project window is displayed as shown
below-
> New to create a new existing project.
> Existing to open an existing project.
> Recent for opening a project that was previously loaded into the IDE
( In which we can develop, run, test, and debug your
application).

➢ This opens the standard EXE projects.


Pointer Picture Box
Label Text Box

Frame Command Button

Check Box Option Button

Combo Box List Box

Hscroll Bar Vscroll Bar

Timer Drive List Box

Dir List Box File List Box

Shape Line

Image Data

OLE
Steps in building a Visual Basic Application

➢ Step 1 : Open the Standard EXE Project.


➢ Step 2 : Design the interface using the controls available in toolbox.
➢ Step 3 : Set properties of the controls (Objects).
➢ Step 4 : Write the event procedures (by selecting object window and
event window).
➢ Step 5 : Save the form (click on file -> save).
➢ Step 6 : Run the application by clicking on the start.

[ To save project ➔ projectname.vbp]


[ To save form ➔ formname.frm ]
Simple Example:
• Design an Application to find sum of two numbers

• Designing part :
• Coding part:
• Execution part:
•Assignment find the
average of two numbers ?
UNIT -2

THE FORMS, CONTROLS


AND MENUS
THE FORMS, CONTROLS
AND MENUS
Form Object

• Form is a container or a window consisting of all


controls that make up the user interface.

• The appearance of the form is shown in the figure. It


consist control menu, title bar and three buttons for
minimizing, maximizing and closing the form.
Control Menu
• It allows moving re-sizing, minimizing, maximizing and
closing of the form.

• In property window (Control box = True) control menu


button is enabled, (Control box = False) control menu
button at the left corner of the title bar is not appeared.

• The restore command restores the form to its original size


before it was maximized.

• When control box is set to false and the caption is an


empty string the title bar totally disappears.
• MINIMIZE AND MAXIMIZE button by default tin
property MinButton and MaxButton is true, if it s
false these buttons are hidden on title bar.

• Startupposition determines the position of the forms


on the screen depending on its value.
Value Description

0 - Manual Initial setting not specified

Centers the form on the item to which it


1 - CenterOwner
belongs.

2 – CenterScreen Centers the form on the whole screen.


3 - WindowsDefault
Form is positioned at upper – left corner of the
screen.
• Window State – The visual state of the form can be
set to maximum, minimum or normal.

Value Description

0 - Normal Normal size of the form created by user.

1 - Minimized Form is minimized to an icon

2 - Maximized Form is maximized to occupy the entire screen


Form Properties

• The form properties which can be set at design time.

• Syntax – ObjectName.PropertyName = Value

• Example – Form1.Caption = “Login Form”


Property Description
Name User – defined name of an object (or an object name)
Appearance Selects 3-D or flat appearance
BackColor Sets the form background color.
Sets the form border to be fixed or
BorderStyle
sizeable.
If True, allows the form to respond to
Enabled mouse and keyboard events; if False,
disables form.
Font Sets font type, style, size.
ForeColor Sets color of text or graphics.
Picture Places a bitmap picture in the form.
Visible If False, hides the form.
Caption Sets message displayed in the title bar of an object
Sets the shapes of the mouse pointer displayed, when
MousePointer
over part of an object
Project --> Project properties
StartUp Form

• Usually VB Application consists of more than single


form.

• The application starts executing the main form or the


startup form loaded.

• The startup form can be controlled by setting the


StartUp object in the project properties window.
• By default, Visual Basic suggests the name of the
form which was first created.

• Every time when a form has to be checked for errors,


the StartUp object must be changed to form name
which has to be tested.
Events
• The three possible states of a form are:
State Description
Not loaded Form is not loaded into memory.
Loaded but not shown Form is loaded into memory but not displayed.
Loaded and shown Form is loaded, displayed and ready to interact.

• Form to load – Load FormName


• Example: Load FrmPic
• From to Unload – UnLoad FormName
• Example: UnLoad FrmPic
Event Description
Activate Form_Activate event is triggered when form
becomes the active window.
Deactivate Occurs when the form is not active window. A form
becomes deactivated when the user switches over to
another form or uses to hide method.
Load Form_Load event occurs when form is loaded.
This is a good place to initialize variables and
set any run-time properties.
UnLoad Form_UnLoad event occurs when form is unloaded.
It is generally used to reset the properties to their original
values.
Click Form_Click event is triggered when user clicks
on form.
DblClick Form_DblClick event is triggered when user
double-clicks on form.
ReSize This event occurs when the user changes the size of the
form.
• Shortcut Keys for Using Visual Basic
Key(s) What the shortcut does
Alt + Q Exits out of Visual Basic
Ctrl + N Creates a new project
Ctrl + O Opens an existing project
Ctrl + P Prints a project
Ctrl + S Saves a file
F7 Displays the code window for the currently selected object
Shift + F7 Displays the object for a particular event procedure.
Shift + F2 Jumps to the definition of the word identified by the cursor.
F2 Displays the Object Browser window
Ctrl + R Displays the Project Explorer window
F4 Displays the properties window
Ctrl + E Displays the menu editor window
Ctrl + D Adds a file to a Visual Basic project
F5 Runs a Visual Basic Project
Methods FormName.Show mode

• Show Method:
• Show method is used to display a form.
• If the form is not loaded, the show method loads the
form firs and hen displays it.
• FormName is the name of the form and mode which
is optional.
• Mode can take values:

0 – Modeless(default)
1 - Modal
• 1 - Modal – A Modal Form is one that does
not allow the application to proceed unless the
form is closed.
• Ex: When a MsgBox is displayed in a modal
Window, it does not allow the user to proceed
until the user clicks on one of the
buttons(default – ok button). Or
form2.show(1)
• 0 - Modeless – Modeless Forms are highly
interactive. They allow the user to switch to
any of its other forms during execution.
Hide Method FromName.Hide
• When an application contains many forms, it is necessary
to hide some of them.

• Hide method only removes the Form from the screen, but
does not unload it from memory.

• The user cannot interact with hidden form but can


manipulate the form.

• Ex: Form2.hide
Form2.Caption=“Hidden Form”
Form2.Show

Instead using show and hide method, visible property of the


form can be set to true or false
• Form2.Visible=False
Form2.Visible=True

• Other methods :
➢ Cls : 1. This method is used to clear the form.
2. Cls method does not clear controls
3. But it clears any information printed or
drawn on the form using the print
statement.

FormName.Cls
ToolBox Controls
• ToolBox Controls contain all custom controls needed
for Visual Basic application.

• Some advanced controls can be added to ToolBox


from menu bar tool, Project → Components.

• The properties of the control can be set either through


code or in the property window
Intrinsic controls
• The default controls which are automatically
displayed in a ToolBox when form is loaded
are known as intrinsic controls.

• Like – TextBox, Command, Label,Combo


etc…….
Text Box
• The text box is the standard control that is used to
receive input from the user as well as to display the
output.

• Can handle string(text) and numeric data but not


images or pictures.

• String in a textbox can be converted to a numeric data


by using the function Val(text).
Properties Description
Appearance Selects 3-D or flat appearance.
BorderStyle Determines type of border.
Font Sets font type, style, size.
MaxLength Limits the length of displayed text (0 value indicates
unlimited length).
MultiLine Specifies whether text box displays single line or
multiple lines.
PasswordChar Hides text with a single character.
SelStart Starting position of selected text (run-time only).
ScrollBars Specifies type of displayed scroll bar(s).
SelLength Length of selected text (run-time only).
SelText Selected text (run-time only).
Tag Stores a string expression.
Text Displayed text.
Text Box Events:

• Change - Triggered every time the Text property


changes.

• LostFocus - Triggered when the user leaves the text


box. This is a good place to examine the contents of a
text box after editing.

• KeyPress - Triggered whenever a key is pressed.


Used for key trapping, as seen in last class.
Text Box Methods:

• SetFocus - Places the cursor in a specified


text box.

Example 1:
• txtExample.SetFocus ' moves cursor to box
named txtExample
Example 2:

Private Sub Command1_Click()


‘To add the values in Textbox 1 and text
box 2
Sum = Val(Text1.Text) + Val(Text2.Text)
‘To display the answer on label 1
Label1.Caption = Sum
End Sub
Command Button.
•It is used invoke response from the user or to invoke
special function on the form.
•Properties :
1. Name: a) Name to the command button is used to
identify it in the visual basic code.
b) No two command buttons in the same form
can have the same name.
2. Style : Style indicates the type of display of the
command button.
Value Description
(Default value) Command
button is displayed as a
0 – Standard
standard button without
graphics.
Displays the command button
1 – Graphical
with graphics.

0 – Standard

1 – Graphical
• Back Color – Back color of a command botton takes effect,
only when the style property is set to graphical.

• Picture – The picture property can be used to display a bit


map, icon,gif or jpeg file…..

• Enable – This property can be set to true or false. When


false command is disabled and cannot be used until set to true.

• Caption – Text entered on the command, if we placed


ampersand symbol (&) before character during runtime can
use keyboard by pressing Alt and underlined character.
• TabIndex - TabIndex value (starting from 0) sets the order
in which focus is recieved by the controls(enabled) on a form.

• TabStop - When set to false, it prevents the user from


tabbing to a control.

• Command Button Events.

Click KeyDown MouseDown

DragDrop Keypress MouseMove

DragOver Keyup
MouseUp
Gotfocus LostFocus
• Command Button Methods.
• The most frequently used method is SetFocus.

Method Description

Drag Used to handle drag and drop option.

Move Used to change position of the command button.

Refresh Used to complete repaint an object

SetFocus Used to focus a specified button or object.

Used to determine the order in which multiple objects


Zorder
appear
Option Button
•Option Button control allows the user to select only
one option from a group on the form.
•If the user wants to select two options ,the options
must be grouped together with in a frame control.
Private Sub Option1_Click ( )
Shape1.Shape = 0
End Sub

Private Sub Option2_Click()


Shape1.Shape = 1
End Sub

Private Sub Option3_Click()


Shape1.Shape = 2
End Sub

Private Sub Option4_Click()


Shape1.Shape = 3
End Sub

Private Sub Option5_Click()


Shape1.Shape = 4
End Sub

Private Sub Option6_Click()


Shape1.Shape = 5
End Sub
• Option Button Properties:
1. Name: a) Name to the Option Button is used to
identify it in the visual basic code.
b) No two Option Button in the same form
can have the same name.
2. Caption: Title to the Option Button on the form.

3. Value: If a value of option button is set to true at


design time, this button will be pre – selected
at run time.
• Option Button Events

Click KeyDown MouseDown

DragDrop Keypress MouseMove

DragOver Keyup MouseUp

Gotfocus LostFocus DblClick

• Option Button Methods

Set focus
Drag Refresh
Move Zorder
Check Box Control

• A check Box is used to express optional features.

• If checked, its value is set to 1 (Check1.value=1) the


feature is used and if unchecked, its value is set to 0
(Check1.value=0) the feature is not used.

• Check box are normally grouped. But single check


Box is also valid.
Check Box Properties
Properties Description
Name Sets the name
Caption Sets the caption
1 – Right Justify
Alignment
0 – Left Justify
Determines the state of the control.
0 – Unchecked(vbunchecked)
Value
1 – Checked(vbchecked)
2 – Grayed(vbgrayed)
0 – Standard
Style 1 – Graphical (in graphical picture can be loaded like command
button)
When style is set to graphical, DisabledPicture displays the
Disabled picture
picture when the check box is disabled.
When the style is graphical if the mouse is pressed, the picture is
Down Picture displayed as in MouseDown Command Button.
Check Box events

Click KeyDown MouseDown

DragDrop Keypress MouseMove

DragOver Keyup MouseUp

Gotfocus LostFocus DblClick


Example:

Private Sub Check1_Click ()


If Check1.Value = 0 Then
Form1.BackColor = vbRed
ElseIf Check1.Value = 1 Then
Form1.BackColor = vbBlue
End If
End Sub
Difference between Check Box and
Command Box
Check Box Option Button

1. In a group single control more than one 1. In a group only one option can be
control can be selected. selected.

2. Value property has 3 settings 2. Value property has 2 settings 1 – on,


1- Checked, 0 – Unchecked, 2 – Grayed 0 - off
PictureBox Control
• Visual Basic provides two way of displaying
pictures on the screen.
1. PictureBox
2. ImageBox

• Picture box are used to display graphics.

• Picture box to group buttons together.


PictureBox Properties
Properties Description

Name Sets the name

1. Sets the graphic to be displayed in the picturebox.


2. During design time the graphic file name is specified in
the properties.
Picture 3. During run time, the load statement is used as follows:

Picture1.picture=LoadPicture(“D:\VBProject\Pics\lilly.Jpg”)

It is Boolean value. When True, the picture box


AutoSize
automatically resized to displayed the entire content.
• Picture Box Events:
• Click - Triggered when a picture box is clicked.
• DblClick - Triggered when a picture box is double-
clicked.

• Picture Box Methods:


• Cls - Clears the picture box.
• Print – Prints
• [ Examples :
• picExample.Cls ' clears the box picExample
• picExample.Print "a picture box" ' prints text string to
picture box information to the picture box.]
• Top layer – Contains all non – graphical controls except
labels.
• Middle layer - Contains picture box and another
graphical controls including labels.
• Bottom layer – Consist of the result of graphical
methods such as circle drawn using circle method.
• PaintPicture – use draw the contents of a PictureBox
control into another PictureBox….The general syntax.

PictureBox.PaintPicture PictureName, x1, y1, w1, h1, x2, y2,


w2,h2
Argument Description

PictureName Name of the source picture name.

Values indicating the co-ordinates of the top left corner of


x1, y1
the destination object to be drawn.

Width1 Value indicates the destination width of the picture.


(w1)
Height1
Value indicates the destination height of the picture.
(h1)
Values indicating the co-ordinates of the top left corner of
x2,y2 the region to be copied or painted. When omitted 0 is
assumed(optional).
Width2 It is optional the value indicates the source width of
(w2) clipping region within the given picture to be painted.
Height2 It is optional the value indicates the source height of
(h2) clipping region within the given picture to be painted.
Example
Private Sub CmdClose_Click()
End
End Sub

Private Sub CmdOk_Click()


Dim I as integer ,J as integer
Picture2.paintpicture Picture1.picture, 0,0,picture1.width,
Picture1.Height, 0, 0,picture2.width, Picture2.Height
End Sub
Image Control
• An image box is very similar to a picture box in
that it allows you to place graphics information on
a form. Image boxes are more suited for static
situations - that is, cases where no modifications will
be done to the displayed graphics.

• Image box graphics can be resized by using the


Stretch property.
• Image Box Properties:

• Picture Establishes the graphics file to display in the


image box.

• Stretch If False, the image box resizes itself to fit the


graphics. If True, the graphic resizes to fit the control
area.

• Image Box Events:

• Click Triggered when a image box is clicked.


• DblClick Triggered when a image box is double-
clicked.
Five types of graphics files can be loaded into a picture box:

File Description

An image represented by pixels and stored as a collection


of bits in which each bit corresponds to one
Bitmap
pixel. Usually has a .bmp extension. Appears in original
size.

A special type of bitmap file of maximum 32 x 32 size. Has


Icon a .ico extension. We’ll create icon files in Class 5.Appears
in original size.

A file that stores an image as a collection of graphical


objects (lines, circles, polygons) rather than pixels.Metafiles
Metafile preserve an image more accurately than bitmaps when
resized. Has a .wmf extension. Resizes itself to fit the picture
box area.
File Description

JPEG (Joint Photographic Experts Group) is a


compressed bitmap format which supports 8 and
JPEG
24 bit color. It is popular on the Internet. Has a
.jpg extension and scales nicely.

GIF (Graphic Interchange Format) is a


compressed bitmap format originally developed
GIF by CompuServe. It supports up to 256 colors and
is popular on the Internet. Has a .gif extension
and scales nicely.
Difference between an Image Box and Picture
box control
Image Box Control Picture Box Control
1. Occupies less memory 1. Occupies more memory
2. Difficult to move images smoothly 2. Produce a smooth display of
around a form. movement of picture.
3. Cannot use paint picture method 3. Paint picture method can be used
to produce several copies of image.
4.Resize the image box it resizes the 4. Resizing the picture box will not
picture contains in it resizes the picture contains in it.
5. When stretch is true, the image fits 5. When auto size is set to true
into the original size of the image picture box itself gets enlarged or
box. compressed depending on the image
size.
6. Does not display border around its 6.Displays border around its picture.
image.
Combo Box
• The combo box is similar to the list box. The
differences are a combo box includes a text box on top
of a list box and only allows selection of one item. In
some cases, the user can type in an alternate response.
Combo Box Properties:

• Combo box properties are nearly identical to those of


the list box, with the deletion of the MultiSelect
property and the addition of a Style property.

Private Sub Form_Load ( )


Combo1.AddItem “Item1”
Combo1.AddItem “Item2”
Combo1.AddItem “Item3”
Combo1.AddItem “Item4”
End Sub
Properties Description
Appearance Selects 3-D or flat appearance
List Array of items in list box portion
ListCount Number of items in list.
ListIndex The number of the most recently selected item in list.
If no item is selected, ListIndex = -1.
Sorted True means items are sorted in 'Ascii' order, else
items appear in order added.
Style Selects the combo box form.
Style = 0, Dropdown combo; user can change
selection.
Style = 1, Simple combo; user can change
selection.
Style = 2, Dropdown list; user cannot change
selection.
Text Text of most recently selected item.
Combo Box Methods:

1. AddItem - Allows you to insert item in list.

2. Clear - Removes all items from list box.

3. RemoveItem - Removes item from list box,


as identified by index of item to remove.
Combo Box Events:

1. Change – Called when text in combobox is changed.

2. DropDown – Called when the combobox drop-down


list is displayed.

3. GotFocus – Called when ComboxBox receives the


focus.

4. LostFocus – Called when ComboxBox loses the


focus.
• Examples
• cboExample.AddItem "This is an added item" '
adds text string to list
• cboExample.Clear ' clears the combo box
• cboExample.RemoveItem 4 ' removes
cboExample.List(4) from list box
List Box

• A list box displays a list of items from which


the user can select one or more items.

• If the number of items exceeds the number that


can be displayed, a scroll bar is automatically
added.
Properties
Properties Description

Appearance Selects 3-D or flat appearance

ListCount Number of items in list.

ListIndex The number of the most recently selected item in list. If no item is selected,
ListIndex = -1.
MultiSelect Controls how items may be selected (0 -none multiple selection not allowed,
1-multiple selection allowed, 2 - group user to extend the selection).
Sorted True means items are sorted in 'Ascii' order, else
items appear in order added.
Selected Array with elements set equal to True or False, depending on whether
corresponding list item is selected.
• List Box Events:

• Click - Event triggered when item in list is clicked.

• DblClick - Event triggered when item in list is


double-clicked.

• Examples
lstExample.AddItem "This is an added item" ' adds text
string to list
lstExample.Clear ' clears the list box
lstExample.RemoveItem 4 ' removes lstExample.List(4)
from list box
• List Box Methods:

• AddItem - Allows you to insert item in list.

• Clear - Removes all items from list box.

• RemoveItem - Removes item from list box,


as identified by index of item to remove.
Private Sub Form_Load ( )

List1.AddItem “Lesson1”
List1.AddItem “Lesson2”
List1.AddItem “Lesson3”
List1.AddItem “Lesson4”

End Sub
Difference between an Combo Box and List
box control
Combo Box List Box

1. It occupies less space on the screen. 1. It occupies more space on the screen.

2. Instead of selecting an item user can 2. The user must select an item from the
type the text on to it. given list only.

3. Multiple selection of items not allowed. 3. Multiple selection of item allowed.

4. Style is 0 – Drop Down, 1 – Simple


4. Style is 0 – Standard, 1 – Checkbox.
Combo, 2 – Drop Down list.

5. Selcount property is not available. Selcount property is available


Frame Control
➢ Frames provide a way of grouping related controls
on a form.

• once a control is drawn within a frame, it can be


copied and pasted to create a control array within that
frame.

• Copy the object. Then, click on the frame. Paste


the object. You will be asked if you want to create
a control array. Answer Yes.

• Option buttons within a frame work as a group


• Frame Properties:
• Caption Title information at top of frame.
• Font Sets font type, style, size.
• Frame Events

Click KeyDown MouseDown

DragDrop Keypress MouseMove

DragOver Keyup MouseUp

Gotfocus LostFocus DblClick

• Frame Methods

Set focus
Drag Refresh
Move Zorder
Line Tool

• The line tool creates simple straight line segments of


various width and color.

Line Tool Properties:

• BorderColor - Determines the line color.


• BorderStyle - Determines the line 'shape'. Lines can
be transparent,solid, dashed, dotted, and
combinations.
• BorderWidth - Determines line width.
Shape Tool

• The shape tool can create circles, ovals, squares,


rectangles, and rounded squares and Rectangles.

• Colors can be used and various fill patterns are


available.
Properties Description
BackColor Determines the background color of the shape (only used when
FillStyle not Solid.
BackStyle Determines whether the background is transparent or opaque.
BorderColor Determines the color of the shape's outline.
BorderStyle Determines the style of the shape's outline. The border can be
transparent, solid, dashed, dotted, and
combinations.
BorderWidth Determines the width of the shape border line.
FillColor Defines the interior color of the shape.
Shape Determines whether the shape is a square, rectangle,
circle, or some other choice.
FillStyle Determines the interior pattern of a shape. Some
choices are: solid, transparent, cross, etc.
Horizontal and Vertical Scroll Bars
• Horizontal and vertical scroll bars are widely used
in Windows applications. Scroll bars provide an
intuitive way to move through a list of information
and make great input devices.

• Both type of scroll bars are comprised of three areas


that can be clicked, or dragged, to change the scroll
bar value.
Properties Description
LargeChange Increment added to or subtracted from the scroll bar Value
property when the bar area is clicked

Max The value of the horizontal scroll bar at the far right and the
value of the vertical scroll bar at the bottom. Can range from
-32,768 to 32,767.

Min The other extreme value - the horizontal scroll bar at the left
and the vertical scroll bar at the top. Can range from -32,768
to 32,767.
SmallChange The increment added to or subtracted from the scroll bar
Value property when either of the scroll arrows is clicked.

Value The current position of the scroll box (thumb) within the
scroll bar. If you set this in code, Visual Basic moves the
scroll box to the proper position. End arrow Scroll box
(thumb) Bar area.
Scroll Bar Events:

➢ Change - Event is triggered after the scroll box's


position has been modified. Use this event to retrieve
the Value property after any changes in the scroll bar.

➢ Scroll - Event triggered continuously whenever the


scroll box is being moved.
Timer Tool
• The timer control allows us to perform a task at a
specified interval or to wait for a specified length of
time.

• The timer tool does not appear on the form while the
application is running.

• Timer tools work in the background, only being


invoked at time intervals you specify.

• This is multi- tasking - more than one thing is


happening at a time.
Timer Properties:

• Enabled - Used to turn the timer on and off. When


on, it continues to operate until the Enabled property
is set to False.

• Interval - Number of milliseconds between each


invocation of the Timer Event.
• Timer Events:

• The timer tool only has one event, Timer. It has


the form:

Sub TimerName_Timer()
..
End Sub
This is where you put code you want repeated every
Interval seconds.
Drive List Box
• The drive list box control allows a user to select a
valid disk drive at run-time.

• It displays the available drives in a drop-down


combo box.

• No code is needed to load a drive list box; Visual


Basic does this for us.

• We use the box to get the current drive identification.


Drive List Box Properties:

• Drive - Contains the name of the currently


selected drive.

Drive List Box Events:

• Change - Triggered whenever the user or


program changes the drive selection.
Directory List Box
• The directory list box displays an ordered,
hierarchical list of the user's disk directories and
subdirectories.

• The directory structure is displayed in a list box.

• Like, the drive list box, little coding is needed to use


the directory list box.
Directory List Box Properties:

• Path - Contains the current directory path.

Directory List Box Events:

• Change - Triggered when the directory selection is


changed.
File List Box
• The file list box locates and lists files in the directory
specified by its Path property at run-time.

File List Box Properties:

• FileName - Contains the currently selected file


name.
• Path - Contains the current path directory.
• Pattern - Contains a string that determines which
files will be displayed. It supports the use of * and ?
Wildcard characters. For example, using *.dat only
displays files with the .dat extension.
File List Box Events:

• DblClick - Triggered whenever a file name is double-


clicked.

• PathChange - Triggered whenever the path changes


in a file list box.
• When the drive selection is changed (drive box
Change event), you should update the directory
path.
• For example, if the drive box is named drvExample
and the directory box is dirExample, use the code:

Private Sub dirExample_Change()


filExample.Path = dirExample.Path
End Sub

Private Sub drvExample_Change()


dirExample.Path = Left$(drvExample.Drive, 1) & ":"
End Sub
• When the directory selection is changed (directory
box Change event), you should update the displayed
file names.
• With a file box named filExample, this code is:

Private Sub filExample_Click()


Dim filename As String
filename = filExample.Path
If Right$(filExample, 1) < > "\" Then
filename = filename & filExample.filename
End Sub
Menu Editor
• Windows application provide groups of
related commands in menus.

• We can create our own menu editor.

• Menus are also intrinsic controls.


Menu control
properties

Menu title

Menu control
list box

Separator bar
• Properties:
• The Caption box is where you type the text that appears
in the menu bar.

• Access keys are defined in the standard way using the


ampersand (&).

• Separator bars (a horizontal line used to separate menu


items) are defined by using a Caption of a single hyphen
(-).

• When assigning captions and access keys, try to use


conform to any established Windows standards.
• The Name box is where you enter a control name for
each menu item.
• This is analogous to the Name property of command
buttons and is the name used to set
• properties and establish the Click event procedure
for each menu item.
• Each menu item must have a name, even separator
bars! The prefix mnu is used to name menu items.
• Sub -menu item names usually refer back to main
menu headings.
• For example, if the menu item New is under the
main heading File menu, use the name mnuFileNew.
• The Index box is used for indexing any menu items
defined as control arrays.

• The Shortcut dropdown box is used to assign


shortcut keystrokes to any item in a menu structure.
• The shortcut keystroke will appear to the right of the
caption for the menu item.

• An example of such a keystroke is using Ctrl+X to


cut text.
• The HelpContextID and NegotiatePosition boxes
relate to using on-line help and object linking
embedding, and are beyond the scope of this
discussion.

• Checked - Used to indicate whether a toggle option


is turned on or off. If True, a check mark appears next
to the menu item.

• Enabled - If True, menu item can be selected. If


False, menu item is grayed and cannot be selected.
• Visible Controls whether the menu item appears in the
structure.

• WindowList Used with Multiple Document Interface


(MDI).

• Menu Editor form is a list box displaying the


hierarchical list of menu items.

• Sub- menu items are indented to their level in the


hierarchy.

• The right and left arrows adjust the levels of menu items,
while the up and down arrows move items within the
same level.
• The Next, Insert, and Delete buttons are used to
move the selection down one line, insert a line above
the current selection, or delete the current selection,
respectively.
Advanced Controls.

• To create the advanced GUI application.

Project → Components → Microsoft Common Dialog Control

OR

Right click on empty tool box → Select Component → Microsoft


Common Dialog Control
Common Dialog Boxes
• To give the user a standard interface for common
operations in Windows-based applications, Visual
Basic provides a set of common dialog boxes.

• Two of which are the Open and Save As


dialog boxes.

• The Common Dialog control is a ‘custom control’


which means we have to make sure some other files
are present to use it.
• The tool is invoked at run-time using one of five
‘Show’ methods. These methods are:

• Method Common Dialog Box Value


• ShowOpen → Open dialog box 1
• ShowSave → Save As dialog box 2
• ShowColor → Color dialog box 3
• ShowFont → Font dialog box 4
• ShowPrinter → Printer dialog box 5

Example:

Text1.Font = CommonDialog1.FontSize
Properties Description
CancelError If True, generates an error if the Cancel button is clicked.
Allows you to use error- handling procedures to recognize that
Cancel was clicked.

DialogTitle The string appearing in the title bar of the dialog box. Default
is Open. In the example, the DialogTitle is Open Example.

FileName Sets the initial file name that appears in the File name box.
After the dialog box is closed, this property can be read to
determine the name of the selected file.

Filter Used to restrict the filenames that appear in the file list box.
Complete filter specifications for forming a Filter can be found
using on- line help. In the example, the Filter was set to allow
Bitmap (*.bmp), Icon (*.ico) , Metafile (*.wmf) , GIF (*.gif) ,
and JPEG (*.jpg) types (only the Bitmap choice is seen).

FilterIndex Indicates which filter component is default. The example uses


a 1 for the FilterIndex (the default value).
Data Time Picker
• It allows the developer to easily access the DateTime
picker.

• Choose the date or time format.

• Show more than one month in the drop-down.

• All mouse events are supported.

• FirstDayOfWeek property : Controls which day to


appear in the leftmost column of the drop-down
portion of the control.
•Show or hide the “today” text on the calendar and set
which date is considered “today”

How to add DTPicker………?

Project → components → Microsoft Windows Common Controls -2 6.0


OLE Control
• Object Linking and Embedding is the ability to
embed a Microsoft word document or a Microsoft
excel with in window application.
Toolbars Control
• The Toolbar provides the user quick access to the
most commonly used functions of a program.
• To build the Toolbar Demo sample project, perform
the following steps:

• 1. open the project, and change the form's caption to


"Toolbar Demo".
• Creating a toolbar in VB requires the
use of two controls:

1. ImageList control, which contains the images that


will be used for the toolbar buttons.

1. Toolbar itself, these controls are two of nine


controls that are part of Microsoft Windows
Common Controls.
Windows Common Controls 6.0
• Image List (ils) • Progress Bar
• Toolbar (tlb) • Slider
• Status Bar (sta) • ListView (lvw)
• TabStrip • TreeView (tre)
(we will use the Tabbed • ImageCombo
Dialog instead)
• 3. Double-click on the ImageList control to bring it
onto the form:

• The ImageList control is not directly accessible by


the user and is not visible at run-time.
• The purpose of the ImageList control is to provide a
repository for images that are used by several
other controls.
• The ImageListmust be used to supply images to the
Toolbar.
• With the ImageList selected, click F4 to bring up the
property list and name the ImageList imlToolbarIcons.
5. Click the Images tab. The Property Pages dialog
looks like the screen-shot below:

• The task at hand now is to repeatedly


click the Insert Picture button, select
an image.

• Navigate to the following


folder: \Program Files\Microsoft
Visual
Studio\Common\Graphics\Bitmaps\
TlBr_W95.

• This folder contains several standard


toolbar images. For the first image,
click New.bmp, as shown in the
screen-shot below:
• Click the Open button to select the
image and dismiss the "Select
picture" dialog box (if you double-
clicked the "New.bmp" file, that
would have the same effect).

• the image appears in the Images


area of the Images tab of the
Property pages, as shown in the
screen shot on the following page.

• In the Key area, typeNew.


• Repeat the above process as you add the following images
(type "Open", "Save", "Print", and "Help", respectively, for
Key):

• Open.bmp
• Save.bmp
• Print.bmp
• Help.bmp
At this point, click OK to dismiss the Property Pages
dialog.
• 6. The next order of
business is to bring the
toolbar control onto the
form. Do so by double-
clicking the Toolbar
control on the toolbox.

• By default, the toolbar


will be aligned with the
top of the form, below
the menu, if you have
one.
7. With the toolbar selected, press F4 to bring up the
regular properties window, and set the (Name)
property totbrMenuOpts.

• The General tab, set ImageList to "imlToolbarIcons"


(the ImageList that was added to this form) –
remember, the toolbar must get its images from the
images stored in an ImageList control. Also, set
the Style property to "1 – tbrFlat" (the default is "0 –
tbrStandard").
Standard Flat
With the Standard style, the buttons of the toolbar With the Flat style, the toolbar buttons remain flat.
appear raised at all times. This is the older, classic When you pass over one of the buttons with the
style. This was the only style available up through mouse, that button will appear raised.
VB 5
8. On the Buttons tab of the Property Pages dialog, click
the Insert Button command button, and set the following
properties:
• To finish setting up the
toolbar, upon each click of
"Insert Button"
Set the properties as follows:
Index Key Style ToolTipText Image
(don't type in –
VB will supply
automatically)

3 Open 0 – tbrDefault Open 2 (or "Open")


4 3 - tbrSeparator
5 Save 0 – tbrDefault Save 3 (or "Save")
6 3 - tbrSeparator
7 Print 0 – tbrDefault Print 4 (or "Print")
8 3 - tbrSeparator
9 Help 0 – tbrDefault Help 5 (or "Help")
10 3 - tbrSeparator
• Now call the mnuFileOpen_Click event
procedure.

Private Sub tbrMenuOpts_ButtonClick(ByVal Button As


MSComCtlLib.Button)

Select Case Button.Key


Case "New"
mnuFileNew_Click
Case "Open"
mnuFileOpen_Click
Case "Save"
mnuFileSave_Click
Case "Print"
mnuFilePrint_Click
Case "Help"
mnuHelpAbout_Click
End Select
10. Run the program. When you click on a toolbar button
("Print" for example), the corresponding menu procedure code
should execute
Control Array
• A control array is group controls that share the same
name type and then same event procedures.

• Draw the control which we want to create a control


array.

• Select → copy → paste

• Dialog box ask do you want to create a control array

• Choose Yes to confirm.


To add control array element at run time .

Syntax:

• Load object(Index %)
• To unload the control
• Unload object(index %)
Ending an Application
• Do not use END any longer
• Unload all loaded forms instead
– SDI - Unload Me for command button or menu
– MDI - Use For Each Next to unload forms
Private Sub Form_Unload(Cancel as Integer)
Dim SingleForm as Form
For Each SingleForm in Forms
Unload SingleForm
Next
End Sub
MDI
Interface Styles
• Multiple Document Interface (MDI)
• Single Document Interface (SDI)
Multiple Document Interface
Parent
Form

Child
Form

Child
Form
Single Document Interface
Independent
Form

Independent
Form
MDI versus SDI
• SDI=Single-Document Interface
– All we have created thus far
– Each form is in a separate window
• MDI=Multiple-Document Interface
– Like Word, Excel, etc.
– Forms display within the window of other forms
MDI (Multiple-Document Interface)
• One Parent window which "contains" other windows.

• Many Child windows "contained within" Parent window.

• Parent and Child may share menus, toolbars With MDI,


when you unload the main form, all child forms are also
unloaded.

• An advantage of using MDI is that the child forms display
within the boundaries of the parent, giving more control
to the user resizing forms.

• Window menu
– Tile, Cascade, and Window List
Creating MDI Projects
• Add MDI form to project
– Project menu, Add MDI form

• Designate all other forms(except Splash form)


as Children (you can have only one MDI)
– Form property MDIChild=True

• Add a Window menu


THANK YOU

You might also like