U - Iv A F VB.N: NIT Dvanced Eatures of ET
U - Iv A F VB.N: NIT Dvanced Eatures of ET
U - Iv A F VB.N: NIT Dvanced Eatures of ET
ADVANCED FEATURES OF
VB.NET
SYLLABUS
4.1 Dialog Boxes
4.1.1 Open File Dialog
4.1.2 Save File Dialog
4.1.3 Font Dialog
4.1.4 Color Dialog
4.1.5 Print Dialog
4.2 Sub Routine and Function
4.2.1 Sub Routine
4.2.2 Function
4.2.3 Pass by value and pass by reference
2
SYLLABUS
4.3Exception Handling
4.3.1 What is error?
4.3.2 Categories of error
4.3.3 Categories of exception handling
4.3.3.1 Structure exception handling
4.3.3.2 Try…Catch ….Finally
4.3.3.3 Common exception handling
4.3.3.4 Message property and to string
method
3
CONT…
4.3.3.5 Throw
4.3.4 Unstructured exception handling
4.3.3.1 On error statement
4.3.3.2 Err object
4.3.3.3 Resume Next
4.4 SDI/MDI
4
4.1DIALOG BOXES
There are many built-in dialog boxes to be used in
Windows forms for various tasks like opening and
saving files, printing a page, providing choices for
colors, fonts, page setup, etc., to the user of an
application.
5
CONT…
6
4.1.1 OPEN FILE DIALOG
It prompts the user to open a file and allows the
user to select a file to open.
The user can check if the file exists and then open
it.
7
CONT…
Property Description
9
EXAMPLE
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
If OpenFileDialog1.ShowDialog < >
Windows.Forms.DialogResult.Cancel Then
PictureBox1.Image =
Image.FromFile(OpenFileDialog1.FileName)
End If
End Sub
10
CONT…
11
4.1.2 SAVE FILE DIALOG
The Save File Dialog control prompts the user to
select a location for saving a file and allows the user to
specify the name of the file to save data.
12
CONT…
Property Description
Gets or sets a value indicating whether the dialog box
AddExtension automatically adds an extension to a file name if the
user omits the extension.
Gets or sets a value indicating whether the dialog box
CheckFileExists displays a warning if the user specifies a file name that
does not exist.
Gets or sets a value indicating whether the dialog box
CheckPathExists displays a warning if the user specifies a path that does
not exist.
DefaultExt Gets or sets the default file name extension.
Gets or sets a string containing the file name selected in
FileName
the file dialog box.
Gets or sets the initial directory displayed by the file
InitialDirectory
dialog box.
13
OverwritePrompt Gets or sets a value indicating whether the Save As
dialog box displays a warning if the user specifies a file
name that already exists.
CONT…
14
EXAMPLE
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog =
Windows.Forms.DialogResult.OK Then
My.Computer.FileSystem.WriteAllText(SaveFileDialog
1.FileName, RichTextBox1.Text, True)
End If
End Sub
15
CONT…
16
4.1.3 FONTDIALOG
It prompts the user to choose a font from among those
installed on the local computer and lets the user
select the font, font size, and color.
It returns the Font and Color objects.
17
CONT…
Property Description
Color Gets or sets the selected font color.
MaxSize Gets or sets the maximum point size a user can select.
MinSize Gets or sets the minimum point size a user can select.
20
CONT…
21
4.1.4 COLOR DIALOG
The Color Dialog control class represents a common
dialog box that displays available colors along with
controls that enable the user to define custom colors.
It lets the user select a color.
22
CONT…
Property Description
Gets or sets a value indicating whether the user can
AllowFullOpen use the dialog box to define custom colors.
24
EXAMPLE
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles
Button1.Click
If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
Label1.ForeColor = ColorDialog1.Color
End If
End Sub
25
4.1.5 PRINT DIALOG
The Print Dialog control lets the user to print
documents by selecting a printer and choosing which
sections of the document to print from a Windows
Forms application.
26
CONT…
27
CONT…
Property Description
29
EXAMPLE
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
PrintDialog1.Document = PrintDocument1
PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.PrinterSettings =
PrintDialog1.PrinterSettings PrintDocument1.Print()
End If
End Sub
30
CONT…
31
4.2 SUB PROCEDURE & FUNCTIONS
A procedure is a group of logically related statements
that is used to perform a specific task.
Using procedure, large application can be divided into
smaller units.
Types of Procedure
Sub Procedure
Function Procedure
Pass by value and Pass by reference
32
4.1.1 SUB PROCEDURES
They're the handy blocks of code that can organize
your code into single-purpose sections to make
programming easier.
Unlike functions, Sub procedures do not return values,
but like functions.
We can pass values to Sub procedures in an argument
list.
33
CONT…
A general procedure is a group of logically related
statements that are used to perform a specific task.
It is also known as sub procedure.
36
CONT…
Calling of Above Function
Call MaximumNo(txtNo1.Text, txtNo2.Text)
or
MaximumNo(txtNo1.Text, txtNo2.Text)
37
4.2.2 FUNCTION PROCEDURE
A Function procedure is a group of logically related
statements that are used to perform a specific task.
We can pass arguments to the function procedure.
It is optional.
It can be either Public, Private or Protected scope.
Default is Public.
Function Name: 39
It is optional.
40
CONT…
Example:
Function MaximumNo (ByVal a As Integer, ByVal b As
Integer)
If a > b Then
Return a
Else
Return b
End if
End Function
42
CONT…
Module paramByval
Sub swap(ByVal x As Integer, ByVal y As Integer)
Dim temp As Integer
temp = x
x=y
y = temp
End Sub
Sub Main()
Dim a As Integer = 100
Dim b As Integer = 200
Console.WriteLine("Before swap, value of a : {0}", a)
Console.WriteLine("Before swap, value of b : {0}", b)
swap(a, b)
Console.WriteLine("After swap, value of a : {0}", a)
Console.WriteLine("After swap, value of b : {0}", b)
Console.ReadLine()
End Sub
End Module 43
CONT…
Output:
44
PASSING PARAMETERS BY REFERENCE
A reference parameter is a reference to a memory
location of a variable.
When you pass parameters by reference, unlike value
parameters, a new storage location is not created for
these parameters.
The reference parameters represent the same memory
location as the actual parameters that are supplied to
the method.
In VB.Net, you declare the reference parameters using
the ByRef keyword.
45
EXAMPLE:
Module paramByref
Sub swap(ByRef x As Integer, ByRef y As Integer)
Dim temp As Integer
temp = x
x=y
y = temp
End Sub
Sub Main()
Dim a As Integer = 100
Dim b As Integer = 200
Console.WriteLine("Before swap, value of a : {0}", a)
Console.WriteLine("Before swap, value of b : {0}", b)
46
CONT…
swap(a, b)
Console.WriteLine("After swap, value of a : {0}",
a) Console.WriteLine("After swap, value of b :
{0}", b) Console.ReadLine()
End Sub
End Module
47
CONT…
Output:
Before swap, value of a :100
Before swap, value of b :200
After swap, value of a :200
After swap, value of b :100
48
4.3 EXCEPTION HANDLING
4.3.1 What is error?
An exception is a problem that arises during the
execution of a program. An exception is a
response to an exceptional circumstance that
arises while a program is running, such as an
attempt to divide by zero.
Exceptions provide a way to transfer control from
one part of a program to another.VB.Net
exception handling is built upon four keywords:
Try, Catch, Finally and Throw.
49
4.3.2 CATEGORIES OF ERRORS
1.Syntax error
2.Logical error
3.Runtime error
50
CONT…
1. Syntax error
Syntax Errors. ... Visual Basic checks your code
as you type it in the Code Editor window and
alerts you if you make a mistake, such as
misspelling a word or using a language element
improperly.
Syntax errors are the most common type
of errors. You can fix them easily in the coding
environment as soon as they occur
51
CONT…
2.Logical error
In computer programming, a logic error is
a bug in a program that causes it to operate
incorrectly, but not to terminate abnormally (or
crash).
A logic error produces unintended or undesired
output or other behavior, although it may not
immediately be recognized as such.
52
EXAMPLE
Dim x As Integer
Dim y As Integer
Dim answer As Integer
x = 10.5
y=3
answer = x * y
TextBox1.Text = answer
When you've added the code to your button, run
your program and test it out.
You'd think that 10.5 multiplied by 3 would give
you the answer 31.5. Click your button. The
53
answer that appears in your textbox is 30!
4.3.3 CATEGORIES OF EXCEPTION HANDLING
Try
[ tryStatements ]
[ Exit Try ]
[ Catch [ exception [ As type ] ]
[ When expression ] [ catchStatements ]
[ Exit Try ] ]
[ Catch ... ]
[ Finally
m,.[ finallyStatements ] ]
55
End Try
4.3.3.1 EXCEPTION HANDLING
An exception is a problem that arises during the
execution of a program.
An exception is a response to an exceptional
circumstance that arises while a program is running,
such as an attempt to divide by zero.
Whenever an Exception is encountered the application
is crashed and its execution is terminated.
56
CONT…
57
4.3.3.2
1) Try
it contains that you want to execute.
It is optional Try statement block may contain
one or more statements.
2) Catch
The exception that is thrown from Try block is
caught by catch block.
Catch block contains code that handles the
Exception.
3) Finally
It contains statements that must execute before
exit from the Try….catch …block 58
CONT…
Example:
Try [ try Statements ]
[ Exit Try ]
[ catch Statements ]
[ Exit Try ] ]
[ Catch ... ]
End Try
59
4.3.3.3 COMMON EXCEPTION CLASS
Exception Class Description
Message Property
using try..catch for error handling and getting
the message displayed as:
Message box. show (ex. to string)
But it gives very long message.
Is it possible just to get only the actual error or
could give my own modified message.
61
CONT…
To String method
To String is often useful in VB.NET programs. It
allows us to provide a custom To String function
on a VB.NET class.
By using an Overrides To String function, we
specify how an instance renders itself as a String.
62
EXAMPLE:
Public Overrides Function To String() As String
Return String. Format("[{0}, {1}]", _a, _b)
End Function
End Class
Sub Main()
Dim p As Perl = New Perl(2, 3)
Console.WriteLine(p)
End Sub
End Module
Output [2, 3]
63
4.3.3.5 THROW
Throwing Objects
You can throw an object if it is either directly or
indirectly derived from the System. Exception
class.
You can use a throw statement in the catch block
to throw the present object as:
Throw
[ expression ]
64
EXAMPLE
Module exceptionProg
Sub Main()
Try
Throw New ApplicationException("A custom
exception _
is being thrown here...")
Catch e As Exception
Console.WriteLine(e.Message)
Finally
Console.WriteLine("Now inside the Finally
Block")
End Try
Console.ReadKey()
End Sub 65
End Module
2.UNSTRUCTURED ERROR HANDLING
Unstructured Error Handling
1) On Error Goto Line
2) On Error Resume Next
3) On Error Goto 0 or -1
4) Err Object
67
EXAMPLE:
68
4.3.4.2 Err Object
69
4.3.4.3 RESUME NEXT
2) On Error Goto 0 and -1
On Error Goto 0 and -1 statement is used
to disable the error trapping mechanism.
It is used to turned ON and OFF of error
trapping mechanism.
71
EXAMPLE OF THE UNSTRUCTURED ERROR
HANDLING
Public Class Form1
Private Sub btnDivison_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles
btnDivison.Click
Dim int1, int2, ans As Integer
int1 = Num1txt.Text
int2 = Num2txt.Text
On Error GoTo Handle 'Global
Error Handling
72
ans = int1 \int 2
EXAMPLE OF THE UNSTRUCTURED ERROR
HANDLING
“On Error GoTo 0 ' If any error is there which is in this
particular statement then at that
time we can use this statement”
“On Error GoTo -1 'It ignore the particular type of
exception.”
anstxt.Text = ans
Num1txt.Text = ""
Num2txt.Text = ""
Handle:
73
EXAMPLE OF THE UNSTRUCTURED ERROR
HANDLING
If (TypeOf Err.GetException() Is DivideByZeroException)
Then
MsgBox("Error Caught..!!")
Resume Next
End If
End Sub
End Class
74
4.4 SDI/MDI
1. Create a Windows Application.
76
CREATING MDI CHILD FORMS