You are on page 1of 5

Handling Mouse Events

Mouse events ocur with mouse


mouse events related with a Controlmovements
class
in forms and
controls. Following are the various
MoaseDown occurs when a mouse button is pressed
- it
MouseEnter it occurs when the
mouse pointer
MouseHover -it occurs when the mouse pointerenters the control
hovers over the control
MouseLeave - it occurs when the mouse pointer leaves
MouseMove - it occurs when the mouse pointer moves the control
MouseUp - it occurs when the over the control
released mouse pointer is over the control and the
mouse button is
MouseVWheel occurs when the mouse wheel moves and the
- it
The event handlers of the control has focus
mouse events get an argument of type
MouseEventArgs object is used for handling mouse events. It has the following
MouseEventArgs. The
" Buttons - indicates the
mouse button pressed properties -
Clicks - indicates the number of clicks
Delta - indicates the number of detents the
mouse wheel rotated
" X- indicates the
x-coordinate mouseclick
of
" Y- indicates the y-coordinate of mouse click
Handling Keyboard Events
Following are the various keyboard events related with a Control class -
" KeDown - occurs when a key is
pressed down and the control has focus
KeyPress - occurs when a key is pressed and the control has focus
" KeyUp - 0ccurs when a key is released while the
control has focus
The event handlers of the KeyDown and KeyUp
This objet has events get an argument of type KeyEventArgs.
the following properties -
" At - it indicates whether the ALT key is pressed
" Control - it indicates whether the CTRL key is pressed
Handled - it indicates whether the event is handled
" KeyCode - stores the keyboard code for the event
KeyData - stores the keyboard data for the event
" KeyValue - sores the keyboard value for the event
Modifers-it indicates which modifier keys (Ctrl, Shif, and/or Al) are pressed
" Shlft- t indicates if the Shifn key is pressed

$1

Scanned by TapScanner
The event handlers of the
This object has the followingKeyDown and
properties KeyUp events get an argument of type
Handled - indicates if the KeyPress event is
handled KeyEventArgs.
KeyChar -stores the character corresponding
to the key pressed
CONSTRUCTOR
A
Constructor is a special type of Sub
AConstructor procedure called when an object is created.
method is called before an object of its
related class is created. If a class have a
constructor,then the object of that class will be initialized
A constructor is like as
instance
automaticaly.
method, but it
explicit return-type, and can be overridden to distinct from a method since it not ever has an
Thev have the job of initializing the provide custom initialization functionality.
that initializes an object to a object's data member s. If you combine a
valid state
your objects should remain in a valid state.and property methods that only allow valid constructor
states.
Syntax
Public Class User
Public Sub New)
End Sub
End Class

Properties of Constructor
1) It always has the name New".
2) It does not return any value.
3) It is creating the object stated by a class and
4) It does not use return type, not even void. placed your customn code it into a valid state.
5) It is automatically called when object of a class is
6) It isalways declaring in public section. created.
1. Default Constructor
A constructor having no parameters or arguments is called as
Default Constructor.
Example:
Module Modulel
Class Student
Public name, location As String
Public Sub New )
name =Alex"
location: "Pune"

End Sub
End Class
Sub Main )
Dim Student As Student New Student (0
Console. WriteLine (Student.name)
52

Scanned by TapScanner
Console. WritelLine (Student. location)
Console. ReadLine )
End Sub
End Module

Output:
Alex
Pune

2. Parameterized Constructor
A
constructor having parameters or arguments is
Example: called as Parameterized
Module Module1 Constructor.
Class Student
Public name,location As String
PublicSub New(ByVal a As String,
name=a
location-b ByVal b As String)
End Sub
End Class
Sub Main)
Dim Student As
Student-New Student(Alex""Pune")
Console.WriteLine(Student.name)
Console.WriteLine(Student.L ocation)
Console.ReadLine)
End Sub
End Module

Output:
Alex
Pune

Overloaded Constructor
A constructor having more (Multiple Constructor)
than one constructor is called as
Constructor. Overloaded Constructor or Multiple
Example
Module Modulel
Class Student
Public Name, location As String
Public Sub New)//Default Constructor
name-"Alex" location=Pune"
End Sub
Public Sub New(By Val a As String, ByVal b As
name-a String)/Parameterized Constructor
location=b
End Sub
End Class
Sub Main)
Dim Student As Student-New Student()/Default Constructor iscalled
I/Parameterized Constructor is called
Dim Student1 As Student-New Student("John'""Mumbai")
53

Scanned by TapScanner
Console.WriteLine(Student.name &"," &Student Location)
Console.ReadLinc()
End Sub
End Module

DESTRUCTORS
Destructors in VB.NET are executed as a
garbage collector will call Finalize() methodprotected method called as
directly.
Finalize (0
method cannot overloaded by but
if
implemented, but cannot Finalize).
call this
The
method
keyword in statement. Finalize(0 should be overridden by
method called automatically when using
required.
The main effort the object is Overrides
no longer
of the destructor is to
the current objects are going to be release resources and inform other
destroyed objects that
Syntax
Protected Overrides Sub Finalize)

End Sub

Example
Module Modulel
Public Class Student
Protected Overrides Sub Finalize()
Console .WriteLine("In Finalize Method")
Console.ReadLine0
End Sub
End Class
Sub Main)
Dim obj As New Student()
End Sub
End Module
End Function

EXCEPTION HANDLING
Exceptions are the incidence of some condition that changes the normal flow of
execution. For example, if your program run out of memory, file does not exist in the
given path, network connections are dropped etc., then it is a Runtime Errors.

There are three types of errors in VB.Net,


Syntax Error: When we write a code that is not permitted by the rules of
For example, incorrect typed keyword or an incorrect syntax. language.
Run Time Error: When a statement attempts an operation that is
out. For example, referencing an object that is inaccessible. Run unbearable
Time errorsto arises
carry
during execution of code.
54

Scanned by TapScanner
Logical Error: When an application does not perform
errors are difficult to find because a logic the way, it
error not generate the error was intended to
massage. perform. These
Structured Exception Handling
Anexception is a
is a response to a problem that occurs during the execution of a
special condition that ar1ses while program, An excenti en
attempt todivide by zero. a program is running, such
as an
Exceptions deliver a method to transter control from one
part ofa program to another.
VB.Net exception handling is built on four
Try - A Try block recognizes a keywords-Try, Catch, Finaly and Throw.
block of
be triggered. It's having one or more Catch code for which specific exceptions will
blocks.
Catch - A program catches an
a program where to handle the exception with an exception handler at the place in
of an exception. problem. The Catch keyword specifies the catching
Finally -The Finally block is used to execute a
an exception is thrown or not. For given set of
example, if you open a statements,
whether an exception is raised or not. it
whether
file, should be closed
Throw -A program throws an exception when a
using a Throw keyword. problem shows up. This is done

55

Scanned by TapScanner

You might also like