Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How VB.NET uses overloaded event handling applications

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how VB.NET uses overloaded event handling applications, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.

Inherited VB.NET overloaded event handlers

When you inherit a component, all members of that component are incorporated into the new class. An event handler is a method that executes in response to a specific event received by a component, and it is inherited along with other component members. The following example shows a typical event handler:

Private Sub Button1_Click

(ByVal sender As System.

Object, ByVal e as _

System.EventArgs) Handles

Button1.Click

Static Counter as Integer = 0

Counter + = 1

MessageBox.Show (this button has been clicked & _

Counter.ToString () & "times.")

End Sub

The above method is executed whenever a Button1.Click event occurs. The Handles clause at the end of the method declaration associates the method with the event. This is a typical structure of event handlers in a component.

In order to overload this method in an inherited class, you must add the Overridable keyword and change the access level to Protected, Protected Friend, or Public. The following example shows an event handler that can be overloaded:

Protected Overridable Sub

Button1_Click (ByVal sender

As System.Object, _

ByVal e as System.EventArgs)

Handles Button1.Click

Static Counter as Integer = 0

Counter + = 1

MessageBox.Show (this button has been clicked & _

Counter.ToString () & "times.")

End Sub

VB.NET overloaded event handlers inherited in the component

VB.NET MonthCalendar Control Application Manual

VB.NET Imports sentence rules and example explanation

The implementation process of obtaining the address code of the network card by VB.NET

VB.NET enumeration increases programming speed

Summary of related Concepts of VB.NET coordinate system

Overloaded inherited event handlers are basically the same as overloaded inheritance methods of any other type, except that when you overload inherited event handlers, you must remove the Handles clause.

Overload methods in inherited components

Add the Overrides keyword to the method declaration.

Note: do not add the Handles clause to the method. The VB.NET overloaded event handler is already associated with an event in the base class, and this association is passed to the inherited class. That is, this method is executed when an event is raised, without the need for additional Handles clauses.

The following example shows how to overload the event handler in the previous example:

Protected Overrides Sub

Button1_Click (ByVal

Sender As System.Object, _

ByVal e as System.EventArgs)

Static Counter as Integer = 0

Counter + = 1

MessageBox.Show ("

The inherited button has been clicked & _

Counter.ToString () & "times.")

End Sub

Why not need the Handles clause

The Handles clause is no longer associated with this method. This is not an oversight, but an important part of the .NET Framework's handling of events. The VB.NET overloaded event handler is already associated with an event in the base class, and this association is passed to the inherited class. That is, this method is executed when an event is raised, without the need for additional Handles clauses. As shown below, if you add the Handles clause, an additional association with the event is created, which causes the method to be executed twice in each event.

'incorrect code

Protected Overrides Sub

Button1_Click (ByVal sender

As System.Object, _

ByVal e as System.EventArgs)

Handles Button1.Click

Static Counter as Integer = 0

'This variable is incremented each time the button is clicked

Twice.

Counter + = 1

'each time the button is clicked, the message box displays

'twice and display inaccurate information.

MessageBox.Show ("

The inherited button has been clicked & _

Counter.ToString () & "times.")

End Sub

Thank you for reading this article carefully. I hope the article "how to use the overloaded event handling application for VB.NET" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report