TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Event Example: AddHandler, RaiseEvent

Use an Event with AddHandler and RaiseEvent. Attach Subs to an Event with AddHandler.
Event. An Event can have many methods attached to it. When that event is raised, all of those methods are executed. The Delegate keyword is used.
With AddHandler and RaiseEvent, we use Events. We use AddHandler to attach a method to an Event instance. And RaiseEvent causes all attached methods to run.
An example. Here we introduce a simple module to explain events. We provide a Delegate Sub called "EventHandler." And an Event called "_show" is an EventHandler instance.

AddHandler: In Main we use the AddHandler keyword to attach EventHandlers to our Event. We create Delegate instances.

AddressOf: We use AddressOf to reference the Important1 and Important2 Subs as targets for the EventHandler delegate instances.

RaiseEvent: We use this keyword to cause an Event to be triggered (raised). The Important1 and Important2 Subs are executed.

Sub
VB.NET program that uses Event, AddHandler, RaiseEvent Module Module1 Delegate Sub EventHandler() Event _show As EventHandler Sub Main() ' Use AddHandler to attach two EventHandlers to Event. AddHandler _show, New EventHandler(AddressOf Important1) AddHandler _show, New EventHandler(AddressOf Important2) ' Use RaiseEvent to run all handlers for this event. RaiseEvent _show() End Sub Sub Important1() ' Do something important. Console.WriteLine("Important1") End Sub Sub Important2() ' Do something else that is also important. Console.WriteLine("Important2") End Sub End Module Output Important1 Important2
With Events, a single entry point can invoke many methods. This is powerful. But in VB.NET we usually use Events in Windows Forms or WPF programs. Events help with GUI development.

Note: In a GUI a button click will cause an event to be raised. We can run any number of methods when a click occurs.

Button
RemoveHandler. This does the opposite of AddHandler. We can remove methods from the internal event handler list of an Event. This keyword is used less often.
WithEvents. Another syntax form can be used to add events in VB.NET. We can use the WithEvents keyword and the Handles keyword on event-handlers.WithEvents
A summary. With Event, AddHandler and RaiseEvent we develop powerful event-based approaches. The Delegate keyword helps with AddHandler. We use it to reference methods as objects.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf