1. Unit 4
VB OBJECTS & GRAPHICS
1
R. BHUVANESWARI
ASST.PROFESSOR
DEPT OF COMPUTER SCIENCE AND APPLICATION
DKM COLLEGE FOR WOMEN
(AUTONOMOUS)
VELLORE
2. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 2
Menus
Windows applications provide groups of related commands in Menus.
These commands depends on the application
Open and Save are frequently found in applications.
Menus are also known as controls but, menus behave differently from
other controls.
Visual Basic provides an easy way to create menus with the Menu
Editor dialog.
An picture of menu editor is given below:
3. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 3
Creating Menus
you invoke this tool from the Menu Editor button on the standard
toolbar or by pressing the Ctrl+E shortcut key or choosing Menu Editor
command in the Tools menu.
The Menu Editor is a Visual Basic dialog box that manages menus in
your programs. With the Menu Editor, you can:
Add new menus
Modify and reorder existing menus
Delete old menus
Add special effects to your menus, such as access keys, check marks, and keyboard Shortcut
How to create a list of menu commands on a form
1. Click the form itself
2. On the Visual Basic toolbar, click the Menu Editor icon, or select Menu Editor from the Tools menu.
3. In the Caption text box, type the menu caption (the name that will appear on the menu bar), and then press
TAB.
4. In the Name text box, type the menu name (the name the menu has in the program code). By convention,
programmers use the mnu object name prefix to identify both menus and menu commands.
5. To add the menu to your program menu bar, click Next. The Menu Editor clears the dialog box for the next
menu item. As you build your menus, the structure of the menus and commands appear at the bottom of the
dialog box.
6. In the Caption text box, type the caption of your first menu command.
7. Press tab, and then type the object name of the command in the Name text box.
8. With this first command highlighted in the menu list box, click the right arrow button in the
Menu Editor. In the Menu list box, the command moves one indent (four spaces) to the right. Click the right
arrow button in the Menu Editor dialog box to move items to the right, and click the left arrow button to move
items to the left.
9. Click Next, and then continue to add commands to your menu.
4. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 4
adding more menus and procedures
To add more menus
1. When you’re ready to add another menu, click the left arrow button to
make the menu flush left in the Menu list box.
2. To add another menu and menu commands, repeat Steps 3 through 9 in
the preceding procedure.
3. When you’re finished entering menus and commands, click OK to close
the Menu Editor.
The Menu Editor closes, and your form appears in the programming
environment with the menus you created.
Adding Event Procedures
after you add menus to your form, you can use event procedures to
process the menu commands.
Clicking a menu command on the form in the programming environment
displays the event procedure that runs when the menu command is
chosen.
5. Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 5
Mouse Events
Visual Basic responds to various mouse events, which are recognized by
most of the controls
The main mouse events are
MouseDown
MouseUp
MouseMove.
Click
Dblclick
MouseDown occurs when the user presses any mouse button
MouseUp occurs when the user releases any mouse button
MouseMove occurs when the user moves mouse from one place to
another into the screen
Click When user presses and releases a mouse button.
Dblclick When user presses and releases and again presses and releases
a mouse button.
These events use the arguments button, Shift, X, Y and they contain
information about the mouse's condition when the button is clicked.
6. MDI
The Multiple Document Interface (MDI) was designed
to simplify the exchange of information among
documents, all under the same roof.
With the main application, you can maintain multiple
open windows, but not multiple copies of the
application.
Data exchange is easier when you can view and
compare many documents simultaneously.
OPEN multiple documents at the same time and allow
the user to switch among them with a mouse-click
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 6
7. Each document is displayed in its own window, and all document
windows have the same behaviour.
The main Form, or MDI Form, isn't duplicated, but it acts as a
container for all the windows,
it is called the parent window.
The windows in which the individual documents are displayed are
called Child windows.
An MDI application must have at least two Form, the parent
Form and one or more child Forms.
Each of these Forms has certain properties.
There can be many child forms contained within the parent
Form, but there can be only one parent Form
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 7
8. HOW TO CREATE MDI FORM
To create an MDI application, follow these steps:
Start a new project and then choose Project >>> Add MDI Form to
add the parent Form.
Set the Form's caption to MDI Window
Choose Project >>> Add Form to add a SDI Form.
Make this Form as child of MDI Form by setting the MDI Child
property of the SDI Form to True. Set the caption property to
MDI Child window.
Visual Basic automatically associates this new Form with the
parent Form. This child Form can't exist outside the parent Form;
in the words, it can only be opened within the parent Form.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 8
9. Parent and Child Menus
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 9
MDI Form cannot contain objects other than child Forms,
but MDI Forms can have their own menus.
10. Dialog Boxes
it is simply a form in a program that contains input controls
designed to receive information
To make your programming faster, Visual Basic includes an
ActiveX control, called CommonDialog.
There are 6 common dialog controls such as open,save,..
If the dialog box you want to use is not included in this ready-
made collection of objects, you can create a new one
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 10
11. working with graphics
Working with graphics is easy in Visual Basic 6.
VB6 gives you the flexibility and power to make graphical
applications in easy steps.
It is rich in graphics related features.
The built-in methods, properties and events allow you to use these
features most effectively.
Graphic methods
The graphic methods allow you to draw on the form and the
PictureBox control.
In Visual Basic 6, graphic methods are only supported by the form
object and the PictureBox control.
common graphic methods are
1. Print 5. PSet
2. Cls 6. Circle
3. Point 7. PaintPicture
4. Refresh 8. TextHeight & width
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 11
12. Graphics Methods
Print: Print is the simplest graphic method in Visual Basic 6. It
prints some text on the form or on the PictureBox control. It
displays texts.
Cls: The Cls method is another simple graphic method that is used
to clear the surface of the form or the PictureBox control. If some
texts are present, you can use the Cls method to remove the texts.
It clears any drawing created by the graphic methods.
Point: The Point method returns the color value from an image for
a pixel at a particular point. This method is generally used to
retrieve color values from bitmaps.
Refresh: The refresh method redraws a control or object.
Generally, controls are refreshed automatically most of the times.
But in some cases, you need to refresh a control’s appearance
manually by explicitly invoking the Refresh method.
PSet: The PSet method sets the color of a single pixel on the
form. This method is used to draw points.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 12
13. Graphic properties
Line: The Line method draws a line. Using the Line method, you can
also draw other geometric shapes such as rectangle, triangle etc.
Circle: The Circle method draws a circle. Using the Circle method,
you can also draw other geometric shapes such ellipses, arcs etc.
PaintPicture: The PaintPicture method displays an image on the
form at run-time.
TextHeight: The TextHeight method returns the height of a
string on the form at run-time.
TextWidth: The TextWidth method returns the width of a string
on the form at run-time.
Graphic properties
The graphic properties are useful while working with the graphic
methods.
the following are the graphic properties:
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 13
14. Graphic properties
DrawMode: The DrawMode property sets the mode of drawing for the appearance of
output from the graphic methods. In the DrawMode property, you can choose from a
variety of values.
DrawStyle: The DrawStyle property sets the line style of any drawing from any
graphic methods. It allows you to draw shapes of different line styles such as solid,
dotted, dashed shapes etc.
DrawWidth: The DrawWidth property sets the line width of any drawing from any
graphic methods. While drawing shapes, you can control the thickness of the lines
using this property.
FillColor: The FillColor property is used to fill any shapes with a color. You may use the
symbolic color constants to fill your shapes. You may also use the color codes as well as
the RGB function.
FillStyle: The FillStyle property lets you fill shapes in a particular filling style.
ForeColor: The ForeColor property is used to set or return the foreground color.
AutoRedraw: Set the AutoRedraw property to True to get a persistent graphics when
you’re calling the graphic methods from any event, but not from the Paint event.
ClipControls: Set the ClipControls property to True to make the graphic methods
repaint an object.
Picture: The Picture property is used to set a picture. Pictures can be set both at
design time and run-time.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 14
15. Graphic example
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 15
Drawing lines
The Line method lets you draw lines in Visual Basic 6.
You need to specify the starting point and the finishing point
of the line in the argument.
You may also specify the color of the line. This is optional,
though.
Example:
The following code example shows how to draw a simple
line using the Line method in Visual Basic 6.
private Sub cmdShow_Click()
DrawWidth = 5
Line (0, 0)-(2000, 2000), vbBlue
End Sub
16. A line with drawing styles: example
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 16
A line with drawing styles
Form’s DrawStyle property is used to draw lines using a particular style.
The constant values of the DrawStyle property are 0 (vbSolid), 1 (vbDash), 2
(vbDot), 3 (vbDashDot, 4 (vbDashDotDot), 5 (vbTransparent) and 6
(vbInsideSolid). The default value is 0, vbSolid.
You may use the numeric constant or the symbolic constant such as vbSolid,
vbDash etc to change drawing styles in your code.
Example :
DrawWidth = 1
DrawStyle = 1
Line (0, 0)-(2000, 2000), vbBlue
DrawStyle = vbDash
Dot Line (100, 900)-(2800, 2800), vbRed
17. Drawing circles
Copyright 1999 Prentice-Hall, Inc. 17
You can draw a circle using the Circle method in Visual Basic 6.
You may also use the Circle method to draw different geometric shapes such as
ellipses, arcs etc.
You need to specify the circle’s center and radius values to draw a circle using
the Circle method.
Example:
Private Sub cmdShow_Click()
DrawWidth = 3
Circle (1800, 1800), 1000, vbRed
End Sub
In the above code, (1800, 1800) is the circle’s center, and the radius value is 1000.
The color constant ‘vbRed’ is an optional argument.
If you want to fill some color on circle you can use following code:
Private Sub cmdShow_Click()
FillStyle = vbSolid
FillColor = &H80C0FF
DrawWidth = 3
Circle (1800, 1800), 1000, vbRed
End Sub
For more example on graphics visit:
https://www.go4expert.com/articles/graphics-visual-basic-6-t30202/
18. Using the Common Dialog Control
Before you can use the CommonDialog control, you need to verify that it is
in your toolbox. If you don’t see the CommonDialog icon, follow this
procedure to add it to the toolbox.
To add the CommonDialog control to the toolbox
1. From the Project menu, click Components.
2. Click the Controls tab.
3. Ensure that the Selected Items Only box is not checked.
4. Place a check mark next to Microsoft Common Dialog Control, and then click
OK.
Exploring MS Visual
Basic 6
Copyright 1999 Prentice-Hall, Inc. 18