These are the actions that are allowed on control or an object.
For Ex: Click, Mouse Move, etc.
These are the methods that provide the functionality or the task to be executed whenever an event has been raised on a control. VB.Net supports two types of Event handlers
Using console application for projecting a windows form
To use Windows Form, the “System.Windows.Forms.Form” class should be used.
Open console application
If we Write Imports System.Windows.Form //gives error
So go to project [menu] à add a reference
In add reference menu select .NET & select System.Windows.Forms & press OK
Hence error is removed automatically
‘‘Demo on using a console application to project win form
Imports System.Windows.Forms
Module Winform’’ Demo
Class FClass
Inherits Form
End Class
Sub main ()
Dim fc as New FClass
fc.Text = “VB 270 WinForm ‘‘Demo”
fc.showDialog ()
End sub
End Module
Inclined to build a profession as VB.Net Developer? Then here is the blog post on, explore VB.Net Training
Whenever an event handler is defined for the objects or controls created at the design time then it is said to be a Static Event Handler.
Note:
To bind a static Event Handler for, a control or an object then it is mandatory to create the objects using the “WithEvents” modifier.
Private sub ControlName_EventName (By Val sender as Object, By Val e as xEveneArgs) handles ControlName.EventName
Statement(s)
- - - - - - -
End sub
Take a New Module
‘Demo on Static Event handling . . . .
Imports System.Windows.Forms
Module Static Event Handling Demo
Class FClass
Inherits Form
WithEvents t as New TextBox
Sub new ()
t.Multitime = true
t.ScrollBars = ScrollBars.Vertical
t.Dock = DockStyle.Fill
Me.Text = “StaticEventHandlingdemo”
Me.Controls.Add(t)
End sub
Private sub t.Keypress (By Val sender As Object, By Val e as KeyPressEventArgs)
Handles t.KeyPress
e.Handled = True ‘Nullify the KeyPress (Don’t take p/p of user)
t.Text$ = “East or West .Net is best
So eat.Net, Drink.Net, sleep.Net, .Net simply rockzzzz”
End sub
End class
Sub main ()
Dim fc as New FClass
fc.ShowDialog ()
End sub
End Module
Whenever an Event Handlers are being defined for the controls that are created at the runtime then it is said to be Dynamic Event Handling.
AddHandler, ControlName.EventName, Address of Method Name
Note:
Syntax:
RemoveHandler, ControlName.EventName, Address of MethodName
Take a New Module
‘Demo on Dynamic Event handling . . . .
Imports System.Windows.Forms
Module DynamicEventHandler
Class FClass
Inherits Form
Sub new ()
Dim f/p as new FlowLayoutPanel
For I as Integer = 1 to 5
Dim b as New button
b.Text = i
AddHandler b.click, address of b_click
fip.Controls.Add(b)
Next
Me.Text = “DynamicEventHandlingDemo”
Me.Control.Add (fip)
End sub
Private sub b_click (By Val sender as Object, By Val e as EventArgs)
Dim b as a new button
B = (Type (sender, Button)
MessageBox.Show (“You have clicked” & b.Text, Me.Text)
End sub
End class
Sub main ()
Dim fc as New FClass
fc.ShowDialog ()
End sub
End Module
Execute
Observation
This is nothing but the object from which the event is raised.
Ex: 1, 2, 3, 4, 5
The sender must object so we must convert
We use C Type
C Type (Sender, Button)
It must be assign to b which means
You liked the article?
Like: 0
Vote for difficulty
Current difficulty (Avg): Medium
TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.