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

A case study of Standard events in GUI programming in C #

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

Share

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

This article mainly introduces the relevant knowledge of "standard event case analysis of GUI programming in C#". Xiaobian shows you the operation process through actual cases. The operation method is simple and fast and practical. I hope this article "standard event case analysis of GUI programming in C#" can help you solve the problem.

What are standard events in GUI

The. NET Framework provides a standard pattern for such program events. Use the EventHandler delegate type under the System namespace.

public delegate void EventHandler(object sender,EventArgs e)

Regarding the declaration of EventHandle delegate type, the following three points need to be noted here:

The first parameter holds a reference to the object that triggered the event

The second parameter holds state information indicating what type applies to the application

Return type is void

The second parameter type EventArgs needs to be specified here. It is not designed to pass parameters. When it is used for event handlers that do not need to pass parameters, it should be ignored. If you need to pass parameters, you must declare a class derived from EventArgs. Use appropriate fields to store the data that needs to be passed.

The Object class and EventArgs class are base classes regardless of the actual type used for the parameter. EventHandler thus provides a signature common to all events and event handlers, with only two parameters, rather than each having a different signature.

Usage of standard events

Here we continue to revise the case in yesterday's article. A Hui subscribes to the newspaper of the newspaper. When the newspaper publishes, A Hui can receive the newspaper to read.

Note here:

Replace Handler with EventHandler when declaring a system definition

The event handler signature in the Ahui subscriber should match the signature of the event delegate (ignored if no data is passed)

The code that triggers the event must be invoked with the appropriate parameter type object

newspaper

/// ///newspaper /// class NewspaperOffice { public event EventHandler StartPublishPaper; //1. Declaring events public void Publish() { Console.WriteLine("Published newspaper! "); StartPublishPaper(this,null); //3. Trigger an event to notify subscribers to receive newspapers and read them. } }

A Hui Subscribers

/// ///A Hui Subscribers /// class AhuiPeople { public AhuiPeople(NewspaperOffice npo) { npo.StartPublishPaper += SubscriptinPaper; //2. Subscribe to events } void SubscriptinPaper(object sender,EventArgs e) { Console.WriteLine("A Hui receives newspaper, starts reading! "); } }

Triggering event. Start transmitting.

static void Main(string[] args) { NewspaperOffice npo = new NewspaperOffice(); AhuiPeople ahuiPeople = new AhuiPeople(npo); //newspaper publication Console.WriteLine("The newspaper plans to start publishing! "); npo.Publish(); Console.ReadKey(); }

output result

About "C#GUI programming standard event case analysis" content introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the industry information channel. Xiaobian will update different knowledge points for you every day.

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