Sei sulla pagina 1di 17

Visual Programming

Unit IV

Common Dialog Box


Common dialog box provide standard interface to work with save, open, font, print and colors.
All these options are accessed using dynamic link library COMMDLG.DLL. This is the control
provided in VB to work with Save, open, print etc.. The control added from component list

Project -> Components-> Microsoft Common Dialog Control 6.0.

The single control is used to access different common dialog boxes. There are different methods
to do this

Method Description
ShowSave Shows save dialog box
ShowOpen Shows open dialog box

ShowColor Show system color dialog box

ShowPrint Shows print dialog box


ShowFont Shows the font dialog box.

The syntax to open the particular dialog box

CommonDialogControl.MethodName

Example:

CommonDialog1.ShowSave ‘ open save dialog box


The user response given on the dialog boxes can be handled in VB code.

Open DialogBox

The ShowOpen method is used to display the dialog to open files. The FileName Property is
used to determine the name of the file the user selected. The Filter Property is used to determine
the types of file to be shown. A Pipe symbol, |, is used to separate the description and value.

Example

The below code shows the Open dialog box. Then selected picture file will load into image
control

Image1

CommonDialog1 – Project- > components - >MS Common Dialog Control 6.0

Command1 – cmdChangeImage

Sub cmdChangeImage_Click()
CommonDialog1.ShowOpen
Image1.Picture = LoadPicture (CommonDialog1.FileName)
End Sub

Run the application


Click the Command button. The open dialog box will be shown. The selected file will be loaded
into the picture control.

Printer Dialog Box

The ShowPrinter method is used to display the dialog to open printer dialog box.The dialog box
allow the user to select the property. All those selected values are sent using Printer object. The
printer properties are

Copies: Specifies the number of copies to be printed.

FromPage: Specifies the value to be displayed in FromPage text box Print dialog box.
ToPage: Specifies the value to be displayed in ToPage text box Print dialog box.
Min: Specifies the minimum value for page range.
Max: Specifies the maximum value for page range.
PrinterDefault:Specifies whether the options selected in Print dialog box change the settings of
Printer system object.

Example

The below code sets few properties of printing and shows the printer dialog box. The result of
which shows the printer dialog box with all the property value as specified

Command1 – cmdSetPrinter
CommonDialog1

Sub cmdSetPrinter_Click()

CommonDialog1.PrinterDefault = True
CommonDialog1.Min = 1
ComminDialog1. Max = 10
CommonDialog1.FromPage = 1
CommonDialog1.ToPage=5
CommonDialog1.ShowPrinter
End Sub

Running the code-> Click the button -> it open the print dialog box with the specified properties
like number of pages, copies etc.,

Font Dialog box

Font dialog box displays the list of available font names and other font styles. This dialog box
can display the fonts supported by either Printer or Screen or Both.
The font properties are

FontBold : Whether bold was selected


FontItalic : Whether italic was selected.
FontStrikethru : Whether strikethrough was selected.
FontUnderline : Whether underline was selected.
FontName : The selected font name
FontSize : The selected font size.

Example

The below code show the font dialog box. The selected font properties are used to change the
form appearance.

Command1 – cmdChangeForm

CommonDialgo1

Sub CmdChangeForm_Click()

CommonDialog1.ShowFont
Me.FontName = CommonDialog1.FontName
Me.FontSize = CommonDialog1.FontSize
Me.FontBold = CommonDialog1.FontBold
Me.FontItalic = CommonDialog1 .FontItalic
Me.FontUnderline = CommonDialog1 .FontUnderline
Me.FontStrikethru = CommonDialog1.FontStrikethru
Print "This is sample text"

End Sub

Color Dialog Box

Color dialog box displays color palette from which user can select a color. Color dialog box also
allows users to create a custom color and select it. Color property returns the selected color.

The below code change the form background to the selected color

Command1 – cmdChangeColor
CommonDialgo1

Sub cmdChagneColor_Click()
CommonDialog1.ShowColor
Me.BackColor = CommonDialog1.Color
End Sub

Run the Code -> Click The button -> it shows the color dialog box- > Select the color. The
selected color is set as form back ground color.

Graphic Functions
The graph methods in VB controls each pixel appear on the screen. The scaling and
AutoRedraw property of form decides scaling area of form the refreshing respectively. The Line
and Shape controls used to draw and print different shapes on the form. There are some drawing
method through which the shapes are drawn.

Line Control

User to draw the line on the form. The line can be draw during the run time also. The most useful
properties of line control are

BorderStyle

BorderWidth

Co-ordinates

Shape Control

It used to draw different shapes. The properties are

Property Description
Shape Determines type of shapge to be drawn

0 – rectangle. It is the default

1- Square
2- Ovel
3- Circle
4- Rounded Rectangle
5- Rounded Square

BackStyle Decides the transparency of shape

0 – Tranparent

1 – Opaque
BorderWidth Thickness of the border Ranges from 0 to
8192
BorderStyle 0 – No Border

1- Solid
2- Dashed Line
3- DottedLine
4- DashDot Line
5- Dash dot dot line

FillColor,FillStype The color to be filled in shape


0 – Solid

Example

The blow code display the respective shape based on the button clicked

Command1 – cmdRectangle

Command2- cmdCircle

Shape1 – grShape

Sub cmdRectangle_Click()

grShape.Shape =0

End Sub

Sub cmdCircle_Click()

grShape.Shape = 3

End Sub

Run the project - > The click event on the button draws the respective shape on the shape
control.
Drawing Methods

There are some method used to draw the shapes at runtime with out shape or line control.

PSet(x,y, [Color]) : The method draw the dot on the screen

PSet(10,20, vbRed) it draw red color dot on 10, 20 co-ordinate

Line(x1,y1, x2, y2, [color]) : The method to draw the line between specified co-ordinates

Line(100,100, 200, 200)

Circle(x1,y1, radius, [color]): Draw the circle from specified coordinates to radius given

Circle(20,40 , 500)

Menu Editor
Menus are used to organization an modules of application based on its process. It make the
application more user friendly. The menu in VB application are created using Menu Editor. It is
available in Tool Menu - > Menu Editor.
Menu editor dialog box contains

Caption Text Box: content given in the caption displayed as menu text

Name Text Box: It is to give the menu name. This is used to identify the menu in code. Same
name can be given to more than one menu if it is added into control array.

Ok or Cancel Button : It is to confirm the menu design. The OK confirms the design. The
Cancel to decline the menu desing.

Index Box: It is to specify the index position of menu if it is in control array.

Shortcut Box: It is used to add accelerator character to menu. Ctrl + key to operate the particular
menu

Windows List Check Box : It is used with MDI windows

Checked Check Box : This is to show the check box in front of menu item.

Enabled Check Box: It decides whether the menu to be enabled or not. The menu is enabled
when this check box is selected else the menu is disabled.

Visible Check Box: This decides whether to show the menu item on the form or not
Arrow Buttons : These button include left, right, up and down arrow. These are all used to
decide the position of menu item.

Next Button : This is to navigate to the next level to insert another menu item.

Insert Button : This is to insert new menu item

Delete Button: This is to remove the selected menu item.

Working with menu

The menu are accessed using the name. The event to be handled for the menu at run time is
‘Click’.

Example

The MDI form includes the menu item. The procedures for the click event of menu item are used
to load the necessary form.

Add MDI From –Make this as Start up object (ProjectProperties->General->Start up Object)

Add Menu as Follows


Sub mnuStudInfo_Click()

frmStudInfo.Load

End Sub

Sub mnuStudHelp_Click()

frmHelp.Load

End Sub

Form1 - Name (frmStudInfo) – Design it as

Code of frmStudInfo

The click event on save button display the student information.

Private Sub cmdSave_Click()


If txtName.Text <> "" And txtRoll.Text <> "" Then
MsgBox "Name : " & txtName.Text & "RollNo : " & txtRoll.Text
Else
MsgBox "Enter the value before save"

End If
End Sub

Add New From: Project Menu -> Add Form -> form : Name (frmHelp) . Design it as
The frmHelp is the information form to display the help support to fill the student information.
This will be shown when help menu is clicked.

Code for frmHelp

This form loaded when help menu is clicked. When the user clicks the Close button after
reading the information the form will be unloaded.

Private Sub cmdClose_Click()


Unload Me

End Sub

Testing and Debugging


Testing is finding faults in programming. Debugging is fixing the fault. Testing is the first step of
debugging. The testing are mostly used to find the logical errors. This is done by running the
program many time with different set of input and verifying output with expected result.

The well structure program make the testing easier. These are all achieved by

1. avoiding nested and complicated control structures,.


2. Avoiding compound conditions.
3. Dividing the program in to modules.

VB IDE (Integrated Development Environment) provides many supporting tools to test and
debug the program. That are

1. Immediate Window
2. Debug menu
1. Immediate Window

This is used to test the programming statement by printing the result immediately. This is
accessed from View Menu -> Immediate Window. The programming line are typed
directly on the immediate window, by pressing Enter key the line get executed and give
the result. More that one lines are separated by colon.

2. Debug Menu

There are many supporting option available in debug menu to test and fix the logical
error in the program. It is accessed Debug menu.

The shortcut of all the debug option can be added into tool bar (View - > ToolBar ->
Debug)
Single Step : In this option VB executes only one time at a time and wait for user
response to continue with next line. The user press F8 to start the single step execution
and to continue with next line. This start the execution from the form_Load. The
currently executing line are highlighted in yellow color. In the mean time the watch with
can be used to see the values in variable which are used in execution of previous line.
Press F5 to stop the single step execution.
Break Point : Particular line of code is marked as break point. The execution stops in
this point (Suspend the execution). The lines marked as break point are indicated with red
color circle to the left of line. Normally the line to be debugged are marked as break
point. This can be done by debug menu or by clicking on the left of line. The break point
are removed by clicking on the red circle or using debug menu. The values of variable or
property executed previously can be watched using watch window. The immediate
window are used to execute any line. The changes in the break point line can be done
before continuing the execution. The restart option are used to resume the execution.

WatchWindow : It is to be added to monitor the changes in the variable during the execution of
program. It is to be opened view->Watch window. The variables are added by clicking the Add
Watch menu under Debug menu or by right clicking in watch window area. This is mostly used
along with the single step execution. In the below code the variable declare on the code added
into watch window.

Potrebbero piacerti anche