【C#】【平时作业】习题-12-事件

1、 什么是事件?

事件(Event) 基本上说是一个用户操作,如按键、点击、鼠标移动等等,或者是一些提示信息,如系统生成的通知。应用程序需要在事件发生时响应事件。例如,中断。

C# 中使用事件机制实现线程间的通信。

2、 事件与委托的关系是什么?

事件是一种特殊的委托的实例,或者说是受限制的委托,是委托一种特殊应用,在类的外部只能施加+=,-=操作符,二者本质上是一个东西。

3、 事件发生的类(事件发生器)的主要内容与作用?

发布器(publisher) 是一个包含事件和委托定义的对象。事件和委托之间的联系也定义在这个对象中。发布器(publisher)类的对象调用这个事件,并通知其他的对象。

4、 事件接收处理的类的主要内容与作用?

订阅器(subscriber) 是一个接受事件并提供事件处理程序的对象。在发布器(publisher)类中的委托调用订阅器(subscriber)类中的方法(事件处理程序)。

5、 什么是订阅事件 ?

定义对象间的一种一对多的依赖关系,当一个主题对象的状态发生改变时, 所有监听它的观察者对象都得到通知并被自动更新。

6、 如何引发事件?

1.创建一个委托
2.将创建的委托与特定事件关联
3.编写事件处理程序
4.利用编写的事件处理程序生成一个委托实例
5.把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件

7、 实现一个捕获键盘按键的程序,该程序包括 KeyEventArgs KeyInputMonitor EventReceiver

using System;
using System.Windows.Forms;

namespace WindowsFormsApp01
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

   
    //(EventArgs是包含事件数据的类的基类)
    internal class KeyInformationEventArgs : EventArgs
    {
        //字段
        private char _keyChar;
        public KeyInformationEventArgs(char keyChar) : base()
        {
            this._keyChar = keyChar;
        }
        //属性
        public char KeyChar
        {
            //只读属性,只有get,无set
            get
            {
                return _keyChar;
            }
        }

    }
    //创建一个事件发生的类KeyInputMOnitor(这个类用于监控文本框的输入并触发一个事件)
    internal class KeyInputMonitor
    {
        //创建委托,返回类型为void,两个参数
        public delegate void KeyInputHandler(Object sender, KeyInformationEventArgs e);
        //定义事件

        //将创建的委托和特定事件关联,在这里特定事件为InputKeyInformation
        public event KeyInputHandler InputKeyInformation;

        public void Run(string response)
        {
            char responseChar = (response == "") ? ' ' : char.ToUpper(response[0]);
            switch (responseChar)
            {
                case 'X':
                    //得到按键信息的参数
                    KeyInformationEventArgs KeyEventArgs1 = new KeyInformationEventArgs(responseChar);
                    //触发事件
                    //委托指定事件处理方法 去 处理事件。这就是 事件接收方 的类的事情了
                    InputKeyInformation(this, KeyEventArgs1);//这一句是触发事件的语句,并将事件交由KeyInputHandler这个委托来处理
                                                             //参数this是指触发事件的对象就是本身这个对象,KeyEventArgs包含了按键信息

                    break;
                case 'Y':
                    KeyInformationEventArgs KeyEventArgs2 = new KeyInformationEventArgs(responseChar);
                    InputKeyInformation(this, KeyEventArgs2);
                    break;
                default:
                    MessageBox.Show("没有触发事件");
                    break;

            }
        }
    }
    //创建一个事件接收方(订阅者)的类
    //这个类先产生一个委托实例。
    //再把这个委托实例添加到产生
    internal class EventReceiver1
    {
        /// <summary>
        /// 定义一个方法
        /// 当事件发生时实际执行的操作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnInputKeyInformation(object sender, KeyInformationEventArgs e)
        {
            //真正的事件处理函数
            string result = string.Format("Key information:{0}", e.KeyChar);
            MessageBox.Show(result);
        }
        /// <summary>
        /// 订阅事件(注册)
        /// 将事件接收方类EventReceiver与产生事件的类KeyInputMonitor关联起来
        /// </summary>
        /// <param name="monitor"></param>
        public EventReceiver1(KeyInputMonitor monitor)
        {
            //订阅(注册)事件
            //产生一个委托实例并添加到KeyInputMonitor产生的事件列表中
            monitor.InputKeyInformation += new KeyInputMonitor.KeyInputHandler(this.OnInputKeyInformation);
        }

    }
    internal class EventReceiver2 {
        private void OnInputKeyInformation(object sender, KeyInformationEventArgs e)
        {
            //真正的事件处理函数
            string result = string.Format("{0}花", e.KeyChar);
            MessageBox.Show(result);
        }
        public EventReceiver2(KeyInputMonitor monitor)
        {
            //订阅(注册)事件
            //产生一个委托实例并添加到KeyInputMonitor产生的事件列表中
            monitor.InputKeyInformation += new KeyInputMonitor.KeyInputHandler(this.OnInputKeyInformation);
        }
    }
        //按钮点击事件
        private void button1_Click(object sender, EventArgs e)
        {
            //实例化一个事件发送器
            KeyInputMonitor monitor = new KeyInputMonitor();
            //实例化一个事件接收器
            EventReceiver1 er = new EventReceiver1(monitor);
            EventReceiver2 er2 = new EventReceiver2(monitor);
            //引发事件运行
            monitor.Run(tb1.Text.Trim());
        }
    }
}

posted @ 2022-09-23 17:27  萌狼蓝天  阅读(41)  评论(0编辑  收藏  举报