Professional Documents
Culture Documents
Unit II VB 6.0 Final
Unit II VB 6.0 Final
Writing a Program
Develop an application for the supermarket SM & CO, when the label tags for the items have to be
obtained in different colours and size.
A program contains a menu bar with two titles: Colours and Size. The Colours menu allows selection of a
colour from a menu and files the program's Form with the selected colour. The Colours menu has menu items
Fillcolour and Exit. When the FillColour menu is clicked another menu pops up with a list of colours. This is a
submenu of the menu FillColour. A menu item can have a maximum of four levels of submenus. The Form of
the program is filled with the selected colour from the popup menu. The Size menu contains menu items Small
and Large.
Example
A new Standard EXE project is opened and the Form and the project files are saved as Colours.frm and
Colours.vbp.
The Form is designed as per the properties table given below
Object Property Settings
Form Caption COLOUR / SIZE
Name frmColour
The menu items are for the Form is designed as per the following specification listed below:
Object Settings
&Colours mnucolour
...&Fill Colour mnuFillColour
......&Red mnuRed
......&Green mnuGreen
......&Blue mnuBlue
&Size mnuSize
... &Small mnuSmall
...&Large mnuLarge
The following steps are performed to create the full menu application.
After selecting the Menu Editor, &Colours is typed in the Caption TextBox and mnucolour in the Name
TextBox. The '&' character underlines the letter C. This property allows to us to press Alt + C while the
program is running which has the same effect of clicking Colours.
The Next button is clicked and &FilI Colour is typed in the Caption TextBox and mnuFillColour in the
Name TextBox. The right arrow button is now clicked.
The Next button is clicked and &Exit is typed in the Caption TextBox and mnuSize the Name TextBox.
The left arrow button is now clicked.
The Next button is clicked and &Size is typed in the Caption TextBox and mnuSize in the Name TextBox.
The left arrow button is now clicked.
Now the next button is clicked and S&maIl is typed in the Caption TextBox and mnuSamll in the Name
TextBox. The right arrow is clicked.
The Next button is clicked and &Large is typed in the Caption TextBox and mnuLarge in the Name
TextBox.
When FillColour menu item is selected, a submenu with Red, Green and Blue should pop-up. Hence, these
items should be included below the Fill Colour item.
Large.Enabled = False
mnusmall.Enabled = True
End Sub
When the menu item Large is selected, the mnuLarge_Chck.( ) procedure is executed which maximizes the size
of the Form and disables the Large menu item. The size of the Form is changed by setting the WindowState
property of the Form.
Private Sub mnuSmall_Click()
frmColor.WindowState = 0
mnuSmall.Enabled = False
mnuLarge.Enabled = True
End Sub
End statement is added in the mnExit_Click( ) To terminate the application. When the menu it Small selected,
mnuSmall_Click() procedure is executed which minimizes the size of the Form and disables the Small menu
item.
Popup Menu
A pop-up menu is a floating menu that is displayed over a Form independent of the menu bar. Pop-up
menus are also called context menu because the items displayed on the pop-up menu depend on where the
pointer is located when the right mouse button is clicked. Any menu can be displayed as a pop-up menu at run
time provided it has one menu item. The following code displays the Colours menu when the user clicks the
right mouse button over the Form at run time.
Private Sub Form_MouseDown(Button As Integer, Shift As
Integer, X As Single, Y s Single)
If Button = 2 Then
PopupMenu mnuCo1ors
End If
End Sub
The first argument is an integer called Button. The value of the argument indicates whether the left, right
or middle mouse button was clicked. The second argument is an integer called shift. The value of this argument
indicates whether the mouse clicked simultaneous with the Shift key, Ctrl key or Alt key. The third and fourth
arguments X and Y are the co-ordinates of the mouse location at the time mouse was clicked. As the Form is
executed automatically whenever the mouse button is clicked inside the Form’s area x, y Co-ordinates are
referenced to the Form.
Positioning a Control
Mouse Down is a commonly used event and it is combined with a Move method to move an
ImageControl to different location in a Form. The following application illustrates the movement of object
responding to events. It makes use of two OptionButton controls, two Image controls and a CommandButton.
The application is designed in such a way that when an OptionButton is selected, the corresponding image
control is placed anywhere in the Form whenever it is clicked.
Example
A new Standard Exe project is opened and the Form and the project files are saved as Move.frm and
Move.vbp. The form is designed as per the properties given below:
Object Property Setting
From Caption Mouse Down Application
Name Form1
OptionButton Caption Credit card is selected
Name Optionl
Value True
OptionButton Caption Cash is selected
Name Option2
Example
A new Standard EXE project is opened and the Form and project files are saved as Draw.frm and Draw.vbp.
The Name and Caption properties of the Form are changed to frmDraw and LINE DRAWING APPLICATION.
The following code is entered in the general declarations of the frmDraw Form,
Option Explicit
The following code is entered in the Form_MouseDown( ) procedure.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer,
X As Single, Y As Single)
frmDraw.CurrentX = X
frmDraw.CurrentY = Y
End Sub
The following code is entered in the Form_MouseMove( ) procedure.
Private Sub Form_MouseMove(Button As Integer, Shift As Integer,
X As Single, Y As Single)
If Button = 1 Then
This program uses two graphics related Visual Basic concepts: for the Line method and the currentX and
CurrentY properties. Line method is preferred for drawing a line in a Form. The following statement draws a
line from the co-ordinates x=2500, y=2000 to x=5000, y = 5500
Line(2500,2000)-(5000,5500)
The CurrentX and CurrentY properties are not visible in the Properties window of the Form because it
cannot be set at the design time. After using the Line method to draw a line in a Form, Visual Basic
automatically assigns the co-ordinates of the line's end point to the CurrentX and Current Y properties of the
Form on which the line was drawn.
Visual Basic does not generate a MouseMove event for every pixel the mouse moves over and a limited
number of mouse messages are generated per second by the operating environment. The following application
illustrates how often the Form_MouseMove( ) procedure is executed.
Example
A new Standard EXE project is opened and the Form and the Project files are saved as MouseMove.frm
and MouseMove.vbp.
Object Property Setting
From Caption Mouse Move Application
Name frmMouseMove
CommandButton Caption &Clear
Name cmdClear
Font System
The following code is entered in the general declaration of the Form.
Option Explicit
The following code is entered in the Form MouseMove event procedures
Private Sub Form_MouseMove()
circle(X, Y), 70
End Sub
The above simply draws small circles at the mouse’s current location using the Circle method. The parameters
x, y represent the centre of the circle and the second parameter represent the radius of the circle.
Private Sub cmdClear_Click()
frmMouseMove.Cls
End Sub
The program is executed by pressing F5. When the mouse inside the Form, circles are drawn along the path of
the mouse movement as shown in below figure. When the clear button is clicked it clears off the circles from
the screen. The circles are widely spaced when the mouse is moved quickly and viva versa. Each small circle is
an indication that the MouseMove event occurred and Form_MouseMove( ) procedure was executed.
The MsgBox( ) function takes the same parameters as the MsgBox statement. The only difference between the
MsgBox statement and MsgBox( ) function is that the function returns a value. The returned value indicates the
button that was clicked in the dialog box.
This application is run by pressing F5. A form appears as shown in below figure. By clicking the Display item,
a dialog box with exclamation point icon and OK button appears. By clicking the Exit menu, a dialog box with
a message appears as shown in above figure. If Yes is clicked, the application terminates.
When date is typed in the TextBox, the procedure determines whether it is valid date or not. If variable I is
filled with characters such as ABC or 1234 the condition. Not IsDate() is satisfied and it displayed a message
box with the message “Invalid Date”.
If the value entered is valid one such as 12/04/18, the condition Not IsDate(1) is not satisfied and the
user is prompted with a message box indicating the day of that date as shown in the below figure:
Two menu items are added to the Form1 of the Dialog.vbp project with the following characteristics. These
menu items are inserted above the Exit menu item.
Caption Name
…Get a Da&y mnuDay
- mnuSep3
The following code is entered in the mnuDay_Click( ) procedure of Form1
Private Sub mnuDay_Click()
Form2.Show 1
If Form2.Tag = “ “ Then
MsgBox “Dialog Box is Cancelled”
Else
MsgBox “The Selected Day is: ” + Form2.Tag
End If
End Sub
When the menu item Get a Day is clicked, it displays the Custom.frm as a custom dialog box and prompts the
user to select a day. It also displays the day which is selected by using the Tag property of the Form2 dialog
box. The code uses Form2.Tag to store the name of the day which is selected.
The following code is entered in the Command2_Click() procedure of Form2 dialog box.
Private Sub Command2_Click()
Form2.Tag = “ “
Form2.Hide
End
End Sub
Whenever the Exit button is clicked, the Tag property of the Form2 dialog box is set to null and the Hide
method hides the Form. Setting Form2.Tag to null indicates that the user has clicked the Exit menu.
Components is selected from the Project menu which displays a Components box.
After ensuring that Common Dialog 6.0 Control CheckBox has a check mark in it, the OK is clicked
Example
A new Standard EXE is opened and the Form and the project are saved as cmdialog.frm and Cmdialog.vbp.
The application is designed and menu items are added to the to the From as per below Tables specifications
The designed Form is represented in below Figure
Object Property Setting
From Caption The Common Dialog Program
Name Form1
CommonDialog Cancel Error True
Name CommonDialog1
Caption Name
&File mnuFile
…Color mnuColour
…Open mnuOpen
- mnuSep
...E&xit mnuExit
Before displaying the dialog box, the mnuColour_Click( ) procedure sets an errortrap. The purpose of an
errortrap is to detect an error during the display of the dialog box. We have set the CancelError Property of the
Dept. of Computer Science, BCAS, Erode 47
Visual Programming – Unit II
CommonDialog1 control to true at design time. Therefore, if Cancel button is pressed when the dialog box is
displayed, the errortrap, which has been set displays the “Dialog Box is Cancelled” and dialog box disappears.
However, if specific colour is selected from the dialog box and OK button is clicked the Back Colour of the
Form is changed to that particular colour. The following code is entered in the mnuOpen_Click( ) procedure.
Private Sub mnuOpen_Click()
Dim filter As String
On Error GoTo errortrap
fi1ter = “all files(*.*) | *.*”
CommonDialogl.filter = fi1ter
CommonDialogl.Action = 1
MsgBox "Selected file is: “+CommonDialog1.filename
Exit Sub
errortrap:
MsgBox “Dialog box is cancelled”
Exit Sub
End Sub
The above procedure displays an Open dialog box. Before displaying the dialog box, the first statement of the
mnuOpen_Click() procedure sets an errortrap which detects an error during the display of the dialog box. If
the Cancel button is pressed when the dialog box is displayed, the errortrap, which has been set, displays the
message “Dialog Box is cancelled” and the dialog box disappears. However, If a specific file is selected from
the dialog box the name of the selected file is displayed as a message box. The filter property of the
CommonDialog1 is set to the value of the filter variable Action property to 1, displays the Open dialog box
Private Sub mnuExit_CliCK )
End
End Sub
The above procedure terminates the application. Like Colour and Open dialog boxes, the properties of the
Common Dialog Control may used to determine the user’s response to the dialog box. Action property 4
displays the Font dialog box and 5 displays the Print dialog box.
RichTextBox Control
The RichTextBox control allows the user to enter and edit text while also providing more advanced
formatting feature than the conventional 'I'extBox control. It almost works like an editor. The RichTextBox
control provides a number of properties we can use to format any portion of text within control. Using these
properties, we can make the text bold or italic, change it colour, and create superscripts and subscripts. We can
also a paragraph formatting by setting both left and right indents, as well as hanging indents.
******
Fundamentals of Graphics
Various controls and methods are used in Visual Basic to draw points, lines, boxes, circles and other
shapes and their location and appearance on a Form can be changed. There are some basic idea that is used for
creating graphics using twips, coordinate system and simple colour.
A twip is a unit that specific the dimensions and location of the object. The number of times a twip is
used by all Visual Basic movement. There are 1440 twips in one inch. These measurements designate the
object’s size then printed. The co-ordinate system is a two-dimensional grid that define location either on the
Form or any other container which or an any other container which is represented as (X, Y). A colour is
represented by a long integer and there are four ways of specifying it at run time; RGB function, using
QBColour function using one of the intrinsic constants in the Object Browser or by entering a colour value
directly.
The RGB( ) function enables the user to specify colours. The RGB( ) function has three arguments. The
value of the first argument represent amount of Red in the final colour the second argument represents Green
and the third represents Blue. The maximum value of each argument is 255 and minimum is 0. The following
statement uses the RGB function to give the colour red.
RGB (255, 0, 0)
The following statement returns yellow colour.
RGB (255, 255, 0)
QBColour function takes a single number that specifies a Quick Base colour number from 0 to 15 and returns a
long integer that can be used in the Visual Basic Colour property.
Forml.BackColor = QBColor (8)
This changes the background colour of the Form to grey.
Line Control
A Line control is a straight line segment that is drawn at design time. The position, length, color and
style of the Line control can be positioned to customize the look of the application.
Shape Control
A Shape control is a visual element that contains several predefined specified shape. In order to view a
specific shape, control is added to the Form by double clicking it. The default shape is rectangle. The Shape
property is selected from the Properties window, which drops a list of shapes, from which a user can select the
desired one.
The FillColor and FillStyle properties of the Shape control can be changed so that the desired color and
style can be obtained.
Image Control
An Image control is a rectangular portion into which picture files can be loaded. Picture files include
bitmap files, icon files and metafiles. A bitmap also called "paint type" graphics defines an image as a pattern of
dots. It has a filename with extension .bmp. An icon is a special kind of bitmap with extension .ico.
Adding Pictures
Line control and Shape control are used for drawing geometric shapes such as lines, circle, squares,
and so on. For drawing more complex figures user can use a picture file. A picture file can be loaded on a
Form, Image control or Picture control. A picture can be added using the following two ways at design time.
In the Properties window of the Form, the Picture property is selected. Visual Basic displays a dialog box
from which a picture file can be selected. The selected picture is displayed in the Form as its background.
Similarly a picture can be loaded in PictureBox and Image control.
A picture can be copied from another application such as Paintbrush to the Clipboard and by selecting Paste
command from the Edit menu, the picture can be pasted onto a Form, Picture Box or Image Control.
Removing Pictures
A picture can be removed at run time using the LoadPicture function without arguments. The following
statement removes a picture from an image control at run time.
Set Image1.Picture = LoadPicture(““)
Stretch Property of an Image control is used to automatically resize a picture and place it inside the
control. When this property is set to True, the image control resizes the picture the desired size to fit into the
control. When this property is set to False, the control automatically adjusts its size to the size of the picture
loaded on to it.
A circle method is used to draw a variety of circular and elliptical shapes. To draw a circle, Visual Basic
requires the location of the circle's centre and the length of its radius. The following statement draws a circle
with a center(1400, 1200) and radius 650.
Circle(l400,1200), 650
The first statement in the above procedure declares a variable called NewForm as a copy of the Child Form
Form1. This implies that, for all purposes, the NewForm can be referred to as an instance of the Form1 with the
same properties that Form1 had at the time of design. The second statement in the procedure causes the newly
created Form to pop up. Every time when the menu item New is clicked in the MDI form, a new Form pops up.
The following code is entered in the mnuTile_Click( ) procedure of the MDI form.
Private Sub mnuTile_Click()
MDIForm1.Arrange vbTi1eHorizonta1
End Sub
The code in the mnuTile_Click() procedure uses the Arrange method with vbTileHorizontal as the argument
to Tile the Child Forms. Below Figure represents the Forms in Tile arrangement. This procedure is executed
when the menu item Tile is clicked. The following code is entered in the mnuCascade_Click( ) procedure of the
MDI form.
The code in the mnuCascade_Click( ) procedure uses the Arrange method with vbCascade as the argument to
cascade the Child Forms. Below Figure represents the cascaded forms. The following code is entered in the
mnuExit_Click( ) procedure.
Private Sub mnuTile_Click()
End
End Sub
When the menu Exit is clicked, the application terminates.
The Me reserved word used in the Form_Rcsizc( ) procedure is a variable containing the name of the
Form where the code is currently executed. The Me keyword in Visual Basic behaves like an implicitly declared
variable. For example, in the above program, there may be several instance of the child form in the parent form.
When the size of one of these forms is changed, the Form_Resize( ) procedure is executed and automatically
updates the Me variable with the instance that was resized.
Creating a Toolbar
Most of the Windows programs include a Toolbar, which is an area containing control to provide quick
access to the most commonly used operations. Toolbar is also called a ribbon bar or control bar. In order to add
a Toolbar item, the MDI form is selected and the Picture control in the Toolbox is double clicked. Visual Basic
responds by displaying a PictureBox control in the Form as shown in below Figure.
User can place text, or a picture, or both in any cell of an MSFlexGrid. The Row and Col properties
specify the current cell in an MSFlexGrid. We can specify the current cell in code, or the user can change it at
run time using the mouse or the arrow keys. The Text property references the contents of the current cell. If a
cell's text is too long to be displayed in the cell, and the WordWrap property is set to True, the text wraps to the
next line within the same cell. To display the wrapped text, we need to increase the cell's column width
(ColWidth property) or row height (RowHeight property). The Cols and Rows properties are used to determine
the number of columns and row in an MSFlexGrid control.
Two kinds of rows or columns are created in the MSFlexGrid control. They are fixed and non-fixed. A
non-fixed row or column scrolls when the scroll bars are active in the MSFlexGrid control. A fixed row or
column does not scroll at any time. FixedRows or FixedCols is generally used for displaying headings. Rows
and columns are created by setting the four properties of the MSFlexGrid control such as Rows, Cols,
FixedRows and FixedCols. Since the MSFlexGrid control is an OCX control, user must make sure whether the
control is included in the project. If the control does not appear in the ToolBox, it is added by selecting
Components from Project menu and placing a check mark in the Microsoft MSFlcxGrid Control. This places
the control in the ToolBox.
Example
This example uses a MSFlexGrid to view the sales of a particular item in a particular month.
Start a new Standard Exe project from the New project dialog box.
The Form is designed as shown below.
To add the FlexGrid, choose Components from the Project menu and check on the Microsoft Flexgrid
Control 6.0.
The two combo boxes are named itemname and mnthname respectively.
The MSFlexgrid is named as itmdet.
The command button with caption Add shown in the above Figure is named as Add.
Dept. of Computer Science, BCAS, Erode 55
Visual Programming – Unit II
In the general declarations section of the Form, the following code is entered to initialize two arrays.
Dim arrl(12) As String
Dim itm(6) As String
Dim i As Integer
The following code is entered in the Form Load event:
itm(0) = “Stationeries”
itm(1) = “Groceries”
itm(2) = “Milk Products”
itm(3) = “Confectionaries”
itm(4) = “House hold item”
itm(5) = “Toys”
arrl (0) = "Jan"
arrl (1) = "Feb"
arrl (2) = "Mar"
arrl (3) = "Apr"
arrl (4) = "May"
arrl (5) = "Jun"
arrl (6) = "Jul"
arrl (7) = "Aug"
arrl (8) = "Sep"
arrl(9) = "Oct"
arrl(10) = "Nov"
arrl(l1) = "Dec"
itmdet.Row = 0
For i = 0 To 11
itmdet.Col = i + 1
itmdet.Text = arrl(i)
mnthname.Addltem arrl(i)
Next
itmdet.Co1 = 0
For i = 0 To 5
itmdet.Row = i + 1
itmdet.Text = itm(i)
itemname.Addltem itm(i)
Next
The two arrays are initialized itm, arrl are initialized in the Form Load. The first "For loop" shown above adds
the names of the month in the 0th row. The second "For loop" adds the names of the items in the 0th column.
The following code is entered in the Add Button’s click event:
itmdet.Row = itemname.Listlndex + 1
itmdet.Col = mnthname.Listlndex + 1
itmdet.Text = Str(Val(itmdet.Text) + Val(sale.Text))
This event procedure stores the value of the sale of the item for the month specified.
Example
A new Standard EXE project is opened and the Form of the project is saved as MSFlexGrid.frm
and the project file is saved as MSFlexGrid.vbp.
MSFlexGrid control is placed in the Form by double clicking it. After placing it, the Rows property of the
MSFlexGrid is set to 7 and Cols property to 4.16.
The Caption property of the Form is changed to MSFLEXGRIDAPPLICATION.
After setting the properties, the MSFlexGrid control is enlarged vertically and horizontally by dragging its
handles.
Now, the Form is designed as per the specification given above looks like the one shown in below Figure. A
menu title File is placed with a menu item Clear in the Form.
The following code is entered in the general declarations section of the Form.
Option Explicit
The following code is entered in the Form_Load( ) procedure of the Form1
Private Sub Form_Load()
FlexGridll.Row = 0
FlexGridll.Col = 1
FlexGridll.Text = "OF"
FlexGridll.Col = 2
FlexGridl.Text = "IMPACT"
FlexGridl.Col = 3
FlexGridl.Text = "IMP_NET"
FlexGridl.Col = 0
FlexGridl.Row = 1
FlexGridl.Text = "Jan-Feb"
FlexGridl.Row = 2
FlexGridl.Text = "Mar-Apr"
FlexGridl.Row = 3
FlexGridl.Text = "May-Jun"
FlexGridl.Row = 4
FlexGridl.Text = "Jul-Aug"
FlexGridl.Row = 5
FlexGridl.Text = "Sep-Oct"
FlexGridl.Row = 6
FlexGridl.Text = "Nov-Dec"
End Sub
The application is run by pressing F5. The program displays a FlexGrid control with seven rows and four
columns displaying the text in the top heading row and the left heading column. The FlexGrid control displays
information in a tabular format. Each cell can be viewed by using the arrow keys but values cannot be entered
directly into cells. The FlexGrid control has both the properties Row and Rows. Similarly it has properties Col
and Cols. Rows and Col properties can be set both during the design time and run tune whereas, the Row and
Col properties are set into action only during run time.
Another procedure called SetRowHeight( ) is added by selecting from the Project menu and it is added
to the code. The following code is entered in the SetRowHeight( ) procedure.
Public Sub SetRowHeight()
Dim counter
For counter = 0 To 6 Step 1
FlexGridl.RowHeight(counter) = 350
Next
End Sub
The code in this procedure uses a "For loop" to change the height of each of the rows to 350 twips. The
RowHeight property determines the height of the cell. The SetRowHeight statement is added to the end of the
Form_Load( ) procedure as shown below.
Private Sub Form_Load()
..................................
.....Same Code written as before.....
SetRowHeight
SetColWidth
End Sub
F1exGrid1.Text = "120"
F1exGridl.Row = 2
FlexGridl.Col = 1
FlexGrid1. Text = "150"
F1exGridl.Row = 3
FlexGrid1.Col = 2
FlexGrid1.Text = “180"
FlexGrid1.Row = 4
FlexGrid1.Co1 = 2
F1exGrid1.Text = "210"
FlexGrid1.Row = 5
FlexGrid1.Co1 = 3
FlexGrid1.Text = "240"
F1exGrid1.Row = 6
FlexGrid1.Col = 3
FlexGridl.Text = "270"
End Sub
The code which is added in the FillValues( ) procedure fills six cells in the FlexGrid by selecting the
Row and Col properties with the required row number and column number, and then sets the Text property of
the cell with the desired text. Below Figure represent a FlexGrid control with values filled in the specified cells.
A FlexGrid control includes a property called FlexGridLines, which can be set to either True or False. The
default setting is True, which causes the control to appear with visible grid lines. If this property is set to False,
the control is shown without grid lines.
*******