zoukankan      html  css  js  c++  java
  • C#委托详细实例(通过窗体应用程序传递消息)

    主窗体->从窗体


    下面根据上图中的步骤进行一步一步地介绍

    //【1】创建委托原型
    //一定要记住委托创建在类的外部
    public delegate void SenderMsgDelegate(string counter);
    
    
    //【2】根据原型委托创建具体的方法
        public void Reciver(string counter)
       {
            this.lblCount.Text = counter;
        }
    //【3】创建委托变量
       public SenderMsgDelegate senderMsgDelegate;
    //【4】根据委托和具体的方法进行关联
       FrmOtherOne frmOtherOne = new FrmOtherOne();
       FrmOtherTwo frmOtherTwo = new FrmOtherTwo();
       FrmOtherThree frmOtherThree = new FrmOtherThree();
       //关联接受者的具体方法
       senderMsgDelegate += frmOtherOne.Reciver;
       senderMsgDelegate += frmOtherTwo.Reciver;
       senderMsgDelegate += frmOtherThree.Reciver;
    
       //显示从窗体
       frmOtherOne.Show();
       frmOtherTwo.Show();
       frmOtherThree.Show();
    
            
    【5】<调用>委托变量传递消息
       private int counter = 0;
       private void btnAdd_Click(object sender, EventArgs e)
      {
          senderMsgDelegate.Invoke(counter++.ToString());
      }

    
    总结
    

       学习C#如果不会用委托那么就是非科班出身。以后学习异步编程和多线程编程都是基于委托实现的。WinForm中的所有事件都是基于委托实现的。要是想要这个Demo的博友可以到我的资源中去下载。





  • 相关阅读:
    [Design]设计模式结构模式
    [Design] 设计模式行为模式
    [Design] Decorator Pattern
    ILIST<T>和LIST<T> 枫
    js 如何调用Windows自带的配色控件 枫
    WML语法全接触 WAP建站语言 枫
    Asp.net模板引擎技术 枫
    smarty内建函数 枫
    NameValueCollection详解 枫
    smarty循环调用问题 枫
  • 原文地址:https://www.cnblogs.com/cqxhl/p/12993316.html
Copyright © 2011-2022 走看看