Delegate and event in VB.net

Source: Internet
Author: User

Trigger events between different projects

Events are triggered between different projects, which cannot be done in VB. HoweverVB. NETWe can use the event and raiseevent keywords and useDelegateKeyword. If you wantVB. NETTo trigger an event in the project and receive the trigger in another project, we must use the event andDelegate.

Execute remote event Source

When we still need to use the raiseevent statement to trigger the eventVB. NETThe code in the project receives the code and we need to use different methods to declare the event. In particular, we need to useDelegateStatement definition eventDelegate.

Create a new class library, name it eventsource, and add a simple class named remoteclass.

Suppose we want to trigger an event, which returns a string parameter. We must first declareDelegate:

PublicDelegateSub remoteeventhandler (byval somestring as string)

GenerallyVB. NETWill automatically create thisDelegate. However, sometimes thisDelegateIt cannot be accessed from other projects, so we should declare it explicitly.

The following is the class we created, which can trigger events:

Public class remoteclass

Public event remoteevent as remoteeventhandler

Public sub dosomething ()

Raiseevent remoteevent (anyevent)

End sub

End Class

The successful implementation of this function depends on the definition of the event itself:

Public event remoteevent as remoteeventhandler

This event does not explicitly define its parameters, but depends onDelegate. Instead, this event is defined as a special type, that is, we just definedDelegate.

In addition, the code used to trigger an event only applies a simple raiseevent statement:

Raiseevent remoteevent (anyevent)

The parameter values provided by this statement are used to return values when an event is triggered.

Receive remote events

In a singleVB. NETIn the project, we can write code classes to receive events. You can add a Windows application project to solve the problem. You can right-click it and select the set as startup project option so that it can be run when you press F5.

To access the class that triggers the event, we must add a reference to the eventsource project. The procedure is as follows: select the menu item project> Add reference ).

Then, we can add a button to the form, open the code window of the form, and introduce the following remote namespace:

Imports system. componentmodel

Imports system. Drawing

Imports system. winforms

Imports eventsource

In Form A, we can use the withevents keyword to define remote classes.

Public class forma

Inherits system. winforms. Form

Private withevents objremote as remoteclass

When the objremote item is selected in the Class Name drop-down table in the upper left corner, you can view the event list in the Method Name drop-down list in the upper right corner of the code window. When this option is selected, the following code will be created:

Public sub objremote_remoteevent () handles objremote. remoteevent

End sub

Unfortunately, the code is incorrect because it does not provide the parameters we passed. To solve this problem, we only need to add parameters to the Declaration:

Public sub objremote_remoteevent (byval data as string )_

Handles objremote. remoteevent

MessageBox. Show (data)

End sub

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.