Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
______________
EVENTS PROGRAMMING BASICS
Learning Objectives
1. Describe events programming and events
2. Describe Visual Studio IDE interfaces (For Simplicity in class we will use
VS2010 instead of VS2019 but to advance is very easy)
3. Understanding basic in VB.NET controls (or components)
4. Describing and writing
- Data types
- Variables
- Modifiers
- Dim statement (Declaration statement)
- Looping statements
- Procedures
General Procedures
Event Procedures
- Functions
- Parameters
5. Event Handling
- Events
- Events Handles
1. Events programming and events
Events are basically a user action like key press, clicks, mouse movements,
etc., or some occurrence like system generated notifications. Applications
need to respond to events when they occur.
Clicking on a button, or entering some text in a text box, or clicking on a menu
item, all are examples of events. An event is an action that calls a function or
may cause another event. Event handlers are functions that tell how to respond
to an event.
Event programming means you programming your program in such way the
flow of code or execution of code depend on user action. VB.Net is an event-
driven language. There are mainly two types of events −
Mouse events
Keyboard events
In programming, an event is an action that occurs as a result of the user or
another source, such as a mouse click. An event handler is a routine that deals
with the event, allowing a programmer to write code that is executed when the
event occurs
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Example of event programming languages
1. Delphi programming language
2. Visual C++ programming language
3. VB.NET programming language
4. C# programming language
5. Java programming language
6. Python programming language
2. Visual Studio IDE interfaces
1. Landing Wizard
2. Project Selection Wizard
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
3. Project Window
4. Toolbox and Properties Window
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
5. Design Window
6. Code Window
7. Project output
When you run (debug) the program
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
3. VB.NET controls (or components)
Control is a graphical gadget or an object or user interface element you drag
and drop on a Visual Basic form by using a toolbox. Each VB.NET Control
has some properties, events, and methods (Event Procedure).
Properties describe the object. For example:
Color
Text
Name
Enable
Width
Height
Visible
Events describe what happens when the user/Object takes any action.
KeyPress event
MouseEnter event
Methods (Event Procedures) are used to make the object do something.
Depend on action code a programmer wants to execute.
Example of basic controls (Components)
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
4. VB.NET Fundamentals
Data types
Determines how much space it occupies in storage and how the bit pattern
stored is interpreted. It also used to give a variable characteristic or type.
Data types available in VB.NET
Data Type Storage Allocation Value Range
Boolean Depends on implementing True or False
platform
Byte 1 byte 0 through 255 (unsigned)
Char 2 bytes 0 through 65535 (unsigned)
Date 8 bytes 0:00:00 (midnight) on January 1, 0001
through 11:59:59 PM on December 31,
9999
Decimal 16 bytes 0 through +/-
79,228,162,514,264,337,593,543,950,335
(+/-7.9...E+28) with no decimal point; 0
through +/-
7.9228162514264337593543950335 with
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
28 places to the right of the decimal
Double 8 bytes
-1.79769313486231570E+308 through -
4.94065645841246544E-324, for
negative values
4.94065645841246544E-324 through
1.79769313486231570E+308, for
positive values
Integer 4 bytes -2,147,483,648 through 2,147,483,647
(signed)
Long 8 bytes -9,223,372,036,854,775,808 through
9,223,372,036,854,775,807(signed)
Object Any type can be stored in a variable of
4 bytes on 32-bit platform
type Object
8 bytes on 64-bit platform
SByte 1 byte -128 through 127 (signed)
Short 2 bytes -32,768 through 32,767 (signed)
Single 4 bytes
-3.4028235E+38 through -1.401298E-45
for negative values;
1.401298E-45 through 3.4028235E+38
for positive values
String Depends on implementing 0 to approximately 2 billion Unicode
platform characters
UInteger 4 bytes 0 through 4,294,967,295 (unsigned)
ULong 8 bytes 0 through 18,446,744,073,709,551,615
(unsigned)
User- Depends on implementing Each member of the structure has a
Defined platform range determined by its data type and
independent of the ranges of the other
members
UShort 2 bytes 0 through 65,535 (unsigned)
Variables
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
A variable is nothing but a name given to a storage area whose size is
determined by data type of that variable
Variable Declaration in VB.Net
The Dim statement is used for variable declaration.
Visual Basic naming rules
Use the following rules when you name procedures, constants, variables, and
arguments in a Visual Basic:
1. You must use a letter as the first character.
2. You can't use a space, period (.), exclamation mark (!), or the characters
@, &, $, # in the name.
3. Name can't exceed 255 characters in length.
4. Dot not user reserved words (Keywords) i. e. function, statement,
method, and intrinsic and constant.
5. You can't repeat names within the same level of scope. For example, you
can't declare two variables named age within the same procedure.
However, you can declare a private variable named age and a procedure-
level variable named age within the same module.
Declaration Statement Syntax
Dim [VariableName] As [datatype]
Example:
Dim StudentName as string
Variable Initialization
Variables are initialized (assigned a value) with an equal sign followed by a constant
expression. The general form of initialization is –
variable_name = value;
For example:
Dim pi As Double
pi = 3.14159
You can initialize a variable at the time of declaration as follows −
Dim StudentID As Integer = 100
Dim StudentName As String = "Bill Smith"
Level of scope For Variables
The following are the methods to represent the scope of a variable in VB.NET.
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
1. Procedure Scope
- Here variable known as local variable
2. Module Scope
- Here variable known as module-level variable
3. Public Scope
- Here variable known as global variable
Modifiers
They provide levels to which a statement can be accessed. Example of the
access modifiers:
Public,
Private,
Protected,
Friend,
Protected Friend,
Modifiers indicate the access level of a programming element like a variable,
constant, enumeration or a class.
Looping statements
VB.Net provides following types of loops to handle looping requirements. Click the
following links to check their details.
Loop Type Description
Do Loop It repeats the enclosed block of statements while a Boolean
condition is True or until the condition becomes True. It could be
terminated at any time with the Exit Do statement.
For...Next It repeats a group of statements a specified number of times and
a loop index counts the number of loop iterations as the loop
executes.
It repeats a group of statements for each element in a collection.
For Each...Next
This loop is used for accessing and manipulating all elements in
an array or a VB.Net collection.
While... End While It executes a series of statements as long as a given condition is
True.
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
With... End With It is not exactly a looping construct. It executes a series of
statements that repeatedly refer to a single object or structure.
Nested loops You can use one or more loops inside any another While, For or
Do loop.
Procedures
Are small unit of codes which accomplishing particular task
therefore are isolated from other codes and are structured in
certain format so that other parts of codes can use them. It is a
standard practice in all programming languages to break your
application down into a set of small units (into blocks of codes).
Visual Basic has, in common with most other languages, both
procedures and functions, which have slightly different properties.
By definition
A procedure is a block of Visual Basic statements enclosed by a
declaration statement (Function, Sub, Operator, Get, Set) and a
matching End declaration. All executable statements in Visual
Basic must be within some procedure.
Structuring your code with procedures gives you the
following benefits:
1. Arrange your code into small understable blocks. Procedures
allow you to break your programs into discrete logical units.
You can debug separate units more easily than you can debug
an entire program without procedures.
2. Reuse of your code. After you develop procedures for use in
one program, you can use them in other programs, often with
little or no modification. This helps you avoid code duplication.
How procedures execute codes
Each time the procedure is called, its statements are executed,
starting with the first executable statement after procedure header
(the Sub statement or Function statement) and ending with the
first End Sub, End Function, Exit Sub, or Return statement
encountered.
Type of Procedures
General Procedures
Property Procedures
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Operator Procedures
Sub Procedures
Function Procedures
Event Handling Procedures
NB: For Certificate level we will discuss only sub, event handling and function
procedures. Remaining procedures are for advance academic levels.
NB: Enclose braces [ ] are used to show that part containing something which may
change depend on programmer code but those individual braces are not part of
procedures structure. For example [modifiers] meaning this part is for modifiers.
1. Sub Procedures
These procedures perform actions but do not return a value to the calling code.
Sub procedure Syntax
2. Function Procedures
These procedures perform actions but return a value to the calling code. They
can perform other actions before returning. This procedure by default they
consist of return statement.
Function procedure Syntax
Function MyFunction(ByVal j As Integer) As Double
Dim answer as Integer
answer = 3.87 * j
return answer
End Function
Function ReturnType
Every Function procedure has a ReturnType. This data type is specified by the
“As” clause in the Function statement, and it determines the data type of the
value the function returns to the calling code.
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Returning Value
The value a Function procedure sends back to the calling code is called its
return value. The procedure returns this value in one of two ways:
A: It uses the Return statement to specify the return value, and returns control
immediately to the calling program. The following example illustrates this.
B: It assigns a value to its own function name in one or more statements of the
procedure. Control does not return to the calling program until an Exit
Function or End Function statement is executed. The following example
illustrates this.
Advantage of method B over A
The advantage of assigning the return value to the function name is that
control does not return from the procedure until it encounters an Exit Function
or End Function statement. This allows you to assign a preliminary value and
adjust it later if necessary.
3. Event Handling Procedures
These are procedures that execute in response to an event raised by user action
or by an occurrence in a program. The code that performs actions in response
to events is written in event procedures. Each event procedure contains the
statements that execute when a particular event occurs on a particular object.
For example, an event procedure named cmdClear_Click will be executed
when the user clicks on the button named cmdClear.
Event handling procedure Syntax
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
[Modifiers] Sub controlname _ eventname [(parameter_List)] Handles [Event_Handler]
statement block
End Sub
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Calling Sub and Function Procedure into Other Codes
You invoke or call a Function or a Sub procedure by including its name (and
arguments if are there) on the right side of an assignment statement or in an
expression.
The syntax for a call to a Function procedure is as follows.
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
a. For a function with arguments
Dim ValueYouWant as Function_ReurnType
ValueYouWant = functionname [( argumentlist )]
b. For a function without arguments
Dim ValueYouWant as Function_ReurnType
ValueYouWant = functionname ()
c. For a sub procedure
Since sub procedures they do not return type the we cannot assign it to any
value but we can call it if we want that block of code to be executed at
certain point.
Subprocedurename ()
Parameters
5. Event Handling
Events
1. Mouse Events
MouseDown
− It occurs when a mouse button is pressed
MouseEnter
− It occurs when the mouse pointer enters the control
MouseHover
− It occurs when the mouse pointer hovers over the control
MouseLeave
− It occurs when the mouse pointer leaves the control
MouseMove
− It occurs when the mouse pointer moves over the control
MouseUp
− It occurs when the mouse pointer is over the control and the mouse
button is released
MouseWheel
− It occurs when the mouse wheel moves and the control has focus
All MouseEvents get argument known as MouseEventArgs
MouseEventArgs has the following properties −
1. Buttons − indicates the mouse button pressed
2. Clicks − indicates the number of clicks
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
3. Delta − indicates the number of detents the mouse wheel rotated
4. X − indicates the x-coordinate of mouse click
5. Y − indicates the y-coordinate of mouse click
2. Keyboard Events
KeyDown
− 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
− occurs when a key is released while the control has focus
Argument of KeyDown and KeyUp events is KeyEventArgs
Argument of KeyPress event is KeyPressEventArgs
KeyEventArgs has the following properties −
1. Alt − it indicates whether the ALT key is pressed
2. Control − it indicates whether the CTRL key is pressed
3. Handled − it indicates whether the event is handled
4. KeyCode − stores the keyboard code for the event
5. KeyData − stores the keyboard data for the event
6. KeyValue − stores the keyboard value for the event
7. Modifiers − it indicates which modifier keys (Ctrl, Shift, and/or
Alt) are pressed
8. Shift − it indicates if the Shift key is pressed
KeyPressEventArgs has the following properties −
1. Handled − indicates if the KeyPress event is handled
2. KeyChar − stores the character corresponding to the key pressed
Events Handles
First events are executed only in event handling procedures therefore you have
first to create event procedure. Any event handling procedure should contain
at least two parameters:
1. Object parameter (sender as object)
2. EventArgs parameter (e as EventArgs)
Lecture One-Events Programming Basics, 2022
Introduction To Event Programming, CIT 206, Mr.
Thobius Joseph, UoI
Assume in figure below and you have button below with the following
properties
1. Text = Add Two Numbers
2. Name = BtnShowResults
And Textbox with name TxtBox1
The event procure structure or syntax in generally is:
[Modifiers] Sub [ObjectName_EventName] ([parameterlist]) handles
ObjectName.EventName
//Action Code
End Sub
1. First Scenario: When you do not want to specific argument to an event.
If you do not need to pass arguments to the event handling procedure (except the sender), then no
subclass of EventArgs is needed. However, note that you should still define the event method
with an argument of type System.EventArgs, and then pass System.EventArgs.Empty or simply
EventArgs.
Now our Event procedure for button clicked event with private modifier would be:
Private Sub BtnShowResults _Clicked (ByVal sender as Object, ByVal e as EventArgs )
handles BtnShowResults.Clicked
Dim Num1 as integer
MessageBox.Show( Num1 + Cint (TxtBox1.Text))
End Sub
2. Second Scenario: When you want to pass specific argument to an event
In this case the argument you passing must be of the type event you are handling. For example if
you pass KeyPressEventArgs then event must be Keypress.
Private Sub BtnShowResults _Clicked (ByVal sender as Object, ByVal e as
KeyPressEventArgs ) handles BtnShowResults.KeyPress
MessageBox.Show( e.KeyChar)
End Sub
- END