0% found this document useful (0 votes)
12 views16 pages

Third

Uploaded by

HEMANTH HP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views16 pages

Third

Uploaded by

HEMANTH HP
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

UNIT – III

MULTIPLE DOCUMENT INTERFACE (MDI)


 MDI stands for Multiple Document Interface. A Multiple Document Interface is
used for opening many windows at the same time.
 All the document windows are contained in a parent window, which provide a
workspace in the applications.
 A child form is an ordinary form that has its child property set to True. Child
forms displayed within the internal area of MDI form.

An MDI Applications consists of


 One MDI Parent form
 One or More MDI Child form(s)
 Optionally independent forms and modules

Creating a Simple MDI applications


1. Start a New Standard.Exe Project
2. Click on Project
3. From the drop-down menu , Click on ‘Add MDI form’
4. A form with the darker color will be displayed. This is the MDI form

To Create an MDI child form , Select Form1 ( Or add a new form) and set its MDI child
Property to True. It is a standard form with following attributes.
1. A Child form cannot be moved outside the parent form’s boundaries
2. Activating a Child form will result in the activation of the parent form
3. When the child form is minimized , its icon will appear at the bottom of the
workspace of the parent form
4. When a child form is maximized , it fills the entire are of the parent form , and
its menu will become the menu of the parent form

Unit - III 53
Visual Basic
5. There can be more than one child form in an application
6. The icons for MDI parent, child and a standard form are different.

Features of An MDI form


 An application can have only one MDI from. If a project already has an MDI form,
the Add MDI form command on the Project menu will be unavailable.
 It can contain only those controls that have an ‘Align’ property or those controls
that are not visible during runtime. This is because the internal area of an MDI
form is defined by the area not covered by these control. These are Toolbar ,
PictureBox, and Data Control.

 To place other control on an MDI form , we can draw a picture box on the form,
and then draw other controls inside a picture box.
 An MDI form object can’t be modal
 The Menu for the Child form will be displayed as the menu on the MDI form
when the Child form has the focus.

Unit - III 54
Visual Basic
 The default AutoShowChildren property displays the child forms automatically
when they are loaded
 The default scrollbars will display the scroll bars when the child forms extend
beyond the boundaries of the parent form.

DEBUGGING TIPS
What is Debugging?
The Process of finding out and removing the error is called debugging.

Types of Bugs (Errors)


Bug
A bug is a coding error in a Computer program
Three are three types of Bugs
1. Errors of Syntax
2. Runtime Errors
3. Logical Error

Errors of Syntax
A syntax error is generated when Visual Basic Editor detects an invalid statement
that violates the Visual Basic syntax rules. For example, misspelled keywords generate
syntax errors. An error dialog box appears that indicates the error message, and
highlights the invalid statement in red.
VBA environment is by default configured to automatically check for syntax
errors. Click on the Tool and Choose options from the Menu. Under the ‘Editor’ tab we
will find the following items checked.
They are code setting and window setting
Code Setting
1. Auto Syntax check
2. Require Variable declaration
3. Auto List members
4. Auto quick info
5. Auto data tips
Window Setting
1. Drag – and – Drop text Editing
2. Default to Full Module view
3. Procedure Separator

Unit - III 55
Visual Basic
Auto Syntax Check
This option used to will check for syntax errors as we enter the commands and
statements.

Require Variable declaration


A Dim , Private , Public , ReDim or Static statement must be declared must be
used to declare a variable before the value is assigned to it. If an undeclared is used ,
an error occurs at compile time.

Auto list Members


This option will show a drop down list box , listing all the members of a control.
When we entered the statement “Dim I as” , a drop – down box appears with all the
possible options we need.

Auto quick Info


This option will provide with the complete syntax for the function that we intend
to write. It is context sensitive and its not necessary to remember thesyntax of every
function by rote.

Auto data tips


We will look at this function when we take up debugging. We can be sure that
syntax errors can be minimized.

Runtime Errors
Runtime error is the execution generated by Visual basic when it ascertains that
the code is about to perform something illegal.

Unit - III 56
Visual Basic
An illegal function could be something as simple as trying to determine the size
of file that does not exist or attempting to multiply two numbers, the result of which
exceeds the storage that could be contained by the data type.

Example
Private Sub Form_Load()
Dim x As Integer
Dim y As Integer
Dim z As Integer
x=9
y=0
z=x/y
End Sub

Logical Errors
logical errors are the most difficult to find, with logic errors , the program will
usually run , but will produce incorrect or unexpected results.
A logic error is a human error - a programming mistake that makes the program code
produce the wrong results

The Debugging Methods


Visual basic provided a number of methods that can be used to make a thorough
check for bugs. The tool and methods include setting Watches , Setting breakpoints ,
stepping through the code using the local window , the immediate window , the stack ,
etc.

The Message Box


Using the message box to display the state of values that have been assigned to
a variable.

Unit - III 57
Visual Basic
Example
Private Sub Command1_Click()
Dim I as Integer
For I = 1 To 20 step 4
Msgbox I
Next I
End Sub

Debug.Print
The other method is to use the Debug object. The debug object will send the
output to the immediate window. The code above can be modifies to look like this.
Example
Private Sub Command1_ Click()
Dim I as Integer
For I = 1 To 20 step 4
Debug. Print I
Next I
End Sub
The Value assigns to the variable ‘I’ will be displayed in the “Immediate Window”.

The Debug Toolbar


The Debug Toolbar Contains buttons that are shortcuts to some commonly used
menu items frequently used in debugging code.
To bring up the Debug toolbar , Right click the Toolbar and Select Debug from
Pop-up menu.

The Debug toolbar has the following buttons


Group 1
 Start
 Break
 End

Unit - III 58
Visual Basic
Group 2
 Toggle Breakpoints
 Step Into
 Step Over
 Step Out
Group 3
 Locals Window
 Immediate Window
 Watch Window
 Quick Watch
 Call stack

Start Button
We start the program by clicking on this button. It is like pressing F5 button and
choosing the Run option
Break Button
It Pauses the program. It is like pressing the ctrl + Break key
End Button
Halts the program. The next sets of buttons come into play only if we set the
Breakpoint
Toggle Break Points
We can have more than one breakpoint, click on this button to shift the
breakpoints
Step Into
Executes code one statement at a time.
Step Over
Click on the Step Over icon , The Called procedure will e executed , and control
will be returned to the next line in the current procedure
Step Out
Click on Step Out icon, control will be transferred to the line after the current
procedure or function.
Local Window
Click on the Locals Window icon. This Window will display all the variables that
are declared in the current procedure. As we continue to step through code , we can
watch the variables that are used in the current procedure.

Unit - III 59
Visual Basic
Immediate Window
This is the window to which the Debug. print sends the output. In this window
we can directly executes a statement as if we are directly communicating with the
interpreter.

Watch Window
In this window all the variables used in the application till the breakpoint are
displayed. The Watch window also displays the name of the procedure where the
variables is used and the value that variables assumes.

Quick Watch Window


Variables may be added to the watch window by highlighting the variable and
selecting "Quick Watch" from the "Debug" menu.

Call stack
The Call Stack window allows we to see the list of procedures that were called
in order to get to the current statement. The window may either be displayed by
selecting "Call Stack" from the "View" menu or by clicking the "Locals" window's
"Call Stack" button.

Unit - III 60
Visual Basic
ERROR HANDLING
Error handling is an essential procedure in Visual Basic programming because it
can help make the program error-free. An error-free program can run smoothly and
efficiently, and the user does not have to face all sorts of problems such as program
crash or system hang.
Errors often occur due to incorrect input from the user. For example, the user
might make the mistake of attempting to ask the computer to divide a number by zero
which will definitely cause system error. Another example is the user might enter a
text (string) to a box that is designed to handle only numeric values such as the weight
of a person, the computer will not be able to perform arithmetic calculation for text
therefore will create an error. These errors are known as synchronous errors.

The Err Object


The VBA engine is always on the lookout for errors that may occur in VB
application. Whenever an error occurs. VBA activities the err object. The err object has
the following properties and methods.

Err object methods


The Clear Method : The err. clear clears the properties of the error object
The Raise method : Helps raise an user – defined error

Err Object Properties


Description Property : This returns a description associated with the error
Help Context Property : Returns a string containing the context ID for a topic
in Help file.
Help File Property : Returns the full filename of the Help File when the user
presses the F1 key.
Number property : This is the default property of the err object. It returns the
number.
Source Property : Returns the name of the procedure or the project where
the error occurred
Last DLL Error property : Returns the name of the called DLL. It also returns a
code to indicate whether the called DLL was success or failure

Start

Unit - III 61
Visual Basic
Is Error
Erro Handler
r

Next Statement

End

Error Trapper
An Error trapper should be introduced in the procedure where we anticipate the
error. Once an error occur , the program flow must be directed to another part of the
procedure will be resolved.

On Error Statement
The On Error statement causes Visual Basic for Applications to start or stop error
trapping. The On Error statement also specifies a set of statements to execute if an error
is encountered.
 On Error go to label
 On Error go to 0
On Error go to Label
This statement tells the VBA to transfer the program control to the line followed
by the label, in case any runtime errors are encountered. In such cases all the statements
between the exception line and the label will not be executed.
Syntax
Sub MySub()
On Error GoTo label
Statements that do something useful
Exit Sub
label:
Error-handling statements
End Sub

Example

Unit - III 62
Visual Basic
Sub GetErr()
On Error GoTo Error_handler:
N = 1 / 0 ' cause an error
MsgBox "This line will not be executed"
Exit Sub
Error_handler:
MsgBox "exception handler"
End Sub

On Error go to 0
This is also called VBA default exception handling. When On Error Goto 0 is in
effect, it is same as having no error handler in the code. Here we are instructing the
program to display the standard runtime message box with ‘Continue’, ‘End’, ‘Debug’
and ‘Help’ buttons.

Using the Error Numbers


By using the error numbers we write a generalized code that can be used by any
of the applications that we may distribute.
The Error Function returns the error message that corresponds to a given error
number.

Standard Error Handler (Or) Error Numbers


3 - Return without GoSub
5 - Invalid procedure call
6 - Overflow
7 - Out of memory
9 - Subscript out of range
10 - This array is fixed or temporarily locked
11 - Division by zero

Unit - III 63
Visual Basic
13 - Type mismatch
14 - Out of string space
52 - Bad file name or number
53 - File not found
55 - File already open
58 - File already exists
68 - Device unavailable
76 - Path not found
94 - Invalid use of Null
Example
Private Sub Command1_Click()
On Error GoTo OpenFileError
Open "A:\JUNK.TXT" For Input As #1
MsgBox "File was opened successfully"
Close #1
Exit Sub
OpenFileError:
MsgBox "The following error occured: " & vbNewLine _
& "Error # " & Err.Number & vbNewLine _
& Err.Description, _
vbCritical, _
"Open Error"
End Sub

COMMON DIALOG CONTROL


The Common dialog control is a custom control that display the commonly used
dialog boxes such as opening, saving, and printing files or selecting colors and fonts
using the Microsoft Windows dynamic link library COMMDLG.DLL.
The Control is visible on the form as an icon at design-time but not at run-time.

Unit - III 64
Visual Basic
The Common Dialog Control Methods
The Common Dialog control can display the following dialogs, using the specified
methods.

Method Dialog Displayed


ShowOpen Show Open Dialog Box
ShowSave Show Save As Dialog Box
ShowColor Show Color Dialog Box
ShowFont Show Font Dialog Box
ShowPrinter Show Print Option Dailog Box
ShowHelp Invokes the Windows Help Engine

Working With Common Dialog Control


1. Right Click on the toolbox and selecting ‘Components’ from the pop-up menu.
Next select ‘Microsoft Common Dialog Control 6.0’ from the list of components.
(OR)
Components is selected from the project menu, Next select ‘Microsoft Common
Dialog Control 6.0’ from the list of components.
2. Start a new project Give it appropriate name.
3. Add the Common Dialog Control on the form
4. Draw the Following Controls on the form
 One textbox Controls
 Five Command Button Control.

Unit - III 65
Visual Basic
File Open Dialog Box
Write some simple code to allow the user to open a file and displays its contents
in the textbox.
Add the following line in the code window of the open command button
CommonDialog1.ShowOpen
There are two ways we can give a title to the dialog box
1. By setting the title at design time
2. By setting the title at runtime through code
Add the following line to the code the Open Command button .
Private Sub Command1_Click()
CommonDialog1.DialogTitle = “Select the File to Open”
CommonDialog1.Showopen
End Sub

Saving a file
The ShowSave method to allow the user to enter the name of the file in which
the text entered in the textbox has to be saved.

Add the following line to the code the Save Command button .
Private Sub Command2_Click()
CommonDialog1.DialogTitle = “Select the File to Open”
CommonDialog1.Showopen
End Sub
Changing the Color
The Common Dialog Control also provides to change the color of the text or other
controls on the form. To display the color Dialog Box we must use the ShowColor
methods.

Add the following line to the code the Color Command button .
Private Sub Command3_Click()
CommonDialog1.ShowColor
Text1.Forecolor = CommonDialog1.Color
End Sub

Printing a document
The Print dialog box allows the user to specify how output should be printed.
The user can specify a range of pages to be printed, print quality, number of copies to
be printed, and so on.

Unit - III 66
Visual Basic
The following example uses the ShowPrinter method with the Printer object to
set the number of copies and page orientation.
Private Sub Command1_Click()
CommonDialog1.ShowPrinter
Printer.Copies = CommonDialog1.Copies
Printer.Orientation = CommonDialog1.Orientation
Printer.EndDoc
End Sub

Rich TextBox Control


The RichTextBox control allows the user to enter and edit text with more
advanced formatting features than the conventional TextBox control.

The RichTextBox control provides a number of properties we can use to apply


formatting to a selected portion of the text within the control. Select the portion of the
text to be formatted and apply the necessary changes.

Working With the RichText Box Control


1. Select the Project / Components menu items
2. Select the Controls tab in the Components box
3. Double click on the Microsoft Rich Textbox control item from the list of
components
4. Close the components box. A new components will appear on the toolbox.
5. Draw the following controls on the form
i. One RichText box control
ii. Six Command Button (Font style , FontColor , Indent , Fileopen ,
Print and Exit)

Unit - III 67
Visual Basic
Changing the font of the selected text
In this case of the Rich Textbox , formatting changes have to be applied to the
text after selecting it. In order to change the font of a portion or all the text in the rich
textbox.
Select the text and call the font dialog box. Select the font type click Ok. The
selected text will be displayed in the New format.

Add the following line to the code the Font Command button .
Private Sub Command3_Click()
CommonDialog1.ShowFont
RichTextBox1.SelFontName = CommonDialog1.FontName
End Sub

Changing the Color of the selected text


To change the color of the selected text or all the text in the rich textbox, the
procedure is similar to that we used for changing the font.
Add the following line to the code the Color Command button .
Private Sub Command4_Click()
CommonDialog1.ShowColor
RichTextBox1.Selcolor = CommonDialog1.color
End Sub

Changing the Indent


The Rich Textbox also we to indent the next. We can have a left indent , a right
indent or a hanging indent.
SelIndent (Default is the left indent) specifies the distance between the left edge
of the Rich TextBox control and left edge of the text that is selected or added.
SelRightIndent specifies the distance between the right edge of the Rich TextBox
control and the right edge of the text.
In order to change the left indent of the selected text we need to provide the
amount of indent required either at runtime or at design time. The amount of indent
has to be gives an integer.
Example

richTextBox1.SelRightIndent = 12

Unit - III 68
Visual Basic

You might also like