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 WithEvents: Handles and RaiseEvent

Use the Handles keyword with WithEvents and RaiseEvent. Implement event handling.
WithEvents. This is a special syntax for events in VB.NET. We can add events with the Handles keyword. We do not need to ever call AddHandler or wire up events.Event
With the WithEvents keyword, we specify a class that contains an event. Then we can use Handles to handle the events in that class. This is powerful.
An example program. Consider this Module. We have a nested class EventClass that contains an Event called TestEvent. The Sub RaiseEvents also is present in EventClass.Sub

WithEvents: This is a special variable on the Module. We create an instance of the EventClass.

Subs: We have 2 Subs that both use the Handles keyword. They handle the TestEvent that is present inside the EventClassInstance variable.

VB.NET program that uses WithEvents, Handles Module Module1 ' An instance of the Event class. WithEvents EventClassInstance As New EventClass Sub PrintTestMessage() Handles EventClassInstance.TestEvent ' This method handles the TestEvent. Console.WriteLine("Test Message Being Printed...") End Sub Sub PrintTestMessage2() Handles EventClassInstance.TestEvent ' This method also handles the event. Console.WriteLine("Test Message 2 Being Printed...") End Sub Sub Main() ' Call into the Event class and raise the test events. EventClassInstance.RaiseEvents() End Sub Class EventClass Public Event TestEvent() Sub RaiseEvents() ' Raise the Test event. ' ... This needs to be part of the class with the Event. RaiseEvent TestEvent() End Sub End Class End Module Output Test Message Being Printed... Test Message 2 Being Printed...
Notes, an advantage. There is a clear advantage of WithEvents-style syntax in VB.NET. We can add new event handlers (like PrintTestMessage2) without calling AddHandler.

So: There is less overhead for adding new event handlers. We just need to add the Sub itself.

A summary. We handled events with the Handles keyword in VB.NET. This syntax form has some advantages over AddHandler—we do not need to use a statement to add each new handler.
© 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