Illustrated C 2005-3英文学习资料.pdfVIP

  • 2
  • 0
  • 约38.48万字
  • 发布于福建
  • 举报
  • 文档已下架,其它文档更精彩
364 C H A P T E R 1 6 ■ E V E N T S Standard Event Usage GUI programming is event driven, which means that while the program is running, it can be interrupted at any time by events such as button clicks, key presses, or system timers. When this happens, the program needs to handle the event and then continue on its course. Clearly, this asynchronous handling of program events is the perfect situation to use C# events. Windows GUI programming uses events so extensively that there is a standard .NET Framework pattern for using them, which you are strongly encouraged to follow. The foundation of the standard pattern for event usage is the EventHandler delegate type, which is declared in the System namespace. The declaration of the EventHandler delegate type is shown in the following code. • The first parameter is meant to hold a reference to the object that raised the event. It is of type object and can, therefore, match any instance of any type. • The second parameter is meant to hold state information of whatever type is appropri- ate for the application. • The return type is void. public delegate void EventHandler(object sender, EventArgs e); Using the EventArgs Class The second parameter in the EventHandler delegate type is an object of class EventArgs, which is declared in the System namespace. You might be tempted to think that, since the second parameter is meant for passing data, an EventArgs class object would be able to store data of some sort. You would be wrong. • The EventArgs class is designed to carry no data. It is used for event handlers that do not need to pass data—and is generally ignored by them. • If you want to pass

您可能关注的文档

文档评论(0)

1亿VIP精品文档

相关文档