当前位置: 首页 > news >正文

第十天 C#学习事件 021

最重要的事件法则:有五个要素  

事件1的写法

 1 class Weituo
 2 {
 3     static void Main(string[] args)
 4     {
 5       Timer timer = new Timer();//事件拥有者
 6         timer.Interval = 800.4;
 7         timer.Elapsed += Boy.Action;//事件(触发)Elapsed   和事件订阅+=
 8         timer.Start();
 9 
10         ConsoleColor c=ConsoleColor.Red;
11         
12         Console.ReadKey();
13     }
14 }
15 
16 class Boy//事件响应者Boy
17 {
18     internal static void Action(object sender, ElapsedEventArgs e)//事件处理器
19     {
20         Random rnd = new Random();
21         int i=rnd.Next(1,24);
22         
23         
24         Console.WriteLine($"我跳!!!获得了{i}个金币");
25     }
26 }


事件二(自己调用自己)字段变化

 1   class Weituo
 2   {
 3       static void Main(string[] args)
 4       {
 5           Form form1 = new Form();//事件拥有者
 6           Controller con1=new Controller(form1);//事件响应者
 7 
 8       form1.ShowDialog();
 9       Console.ReadKey();
10      
11       }
12   }
13 
14 
15   class Controller
16   {
17       private Form form;
18       public Controller(Form form)
19       {
20           if (form !=null)
21           {
22               this.form = form;
23               this.form.Click += this.formClicked;//事件Ckick   和事件订阅+=
24           }
25       }
26 
27       private void formClicked(object sender, EventArgs e)//事件的处理器
28       {
29           this.form.Text = DateTime.Now.ToString();
30       }
31   }

第三种:自己触发自己

 1     class Weituo
 2     {
 3         static void Main(string[] args)
 4         {
 5             MyForm form=new MyForm();//事件拥有者   响应者
 6             form.Click += form.FormClicked;//事件触发器form.Click 和+=订阅符号
 7         }
 8     }
 9 
10 class MyForm : Form//事件响应者(是自己)
11 {
12     internal void FormClicked(object sender, EventArgs e)//事件处理器
13     {
14         this.Text = DateTime.Now.ToString();
15     }
16 }

第四种:Form触发自己的

 1 class Weituo
 2 {
 3     static void Main(string[] args)
 4     {
 5         MyForm1 myform=new MyForm1();
 6         myform.ShowDialog();    
 7     }
 8 }
 9 
10 
11 class MyForm1 : Form
12 {
13     private Button button1;
14     private TextBox textBox1;
15     public MyForm1()
16     {
17         this.button1 = new Button();
18         this.textBox1 = new TextBox();
19         this.Controls.Add(this.button1);
20         this.Controls.Add(this.textBox1);
21         this.button1.Click += this.ButtonClick;
22         this.button1.Text= "Click";
23         this.button1.Top = 50;
24     }
25 
26     private void ButtonClick(object sender, EventArgs e)
27     {
28         Console.WriteLine(sender.ToString());
29     }
30 }

委托和事件方法

image

 

http://www.agseo.cn/news/85/

相关文章:

  • 事半功倍是蠢蛋52 使用docker-compose.override.yml
  • Elasticsearch
  • MySQL单表查询DQL
  • PyQt5 之QMenu菜单栏
  • [TJOI2015] 概率论 题解
  • Linux进程与线程
  • 事半功倍是蠢蛋51 大上黑白屏反色
  • Linux 启动耗时优化 1s 内启动(RK3588 平台)
  • 周总结报告5
  • 使用模拟库进行测试的意义是什么?
  • MyEMS:开源领域的能源管理创新解决方案
  • 【Containerd交互命令】ctr、crictl常用基本命令
  • DAG Matters! GFlowNets Enhanced Explainer For Graph Neural Networks | |
  • abap字符串操作
  • [完结16章]COZE AI 智能体开发体系课(从入门到高级)零基础零代码
  • 推出其新一代高性能Sub-GHz射频收发芯片UM2011A
  • 在 Athena UDF 中使用 Java 将数据写入 DynamoDB
  • Pychram 激活
  • 掌控AI编程全链路:Cline让你随意选模型、透明成本、零信任安全 - 公众号
  • 数据库事务隔离级别引发的应用安全竞态条件漏洞分析
  • Node-Red学习笔记
  • 隧道工程LoRa无线监测设备集成方案 直击隧道深部监测痛点
  • 【k8s】为什么ctr导入后通过crictl查看不到导入的镜像
  • Swift 结合 Tesseract 进行验证码识别
  • 当虚拟机目录空间不足时的扩容
  • 使用IText创建PDF
  • MyEMS 深度解析:碳管理赋能与系统集成的实践路径
  • uv包管理 - 小学弟
  • 对口型视频创作指南:AI如何让“假唱”变成真艺术?
  • 用Python + Tesseract OCR:验证码识别全流程解析