Using proxy as Event

    we use delegate method as event

    first we declared a delegate

    public delegate void Mydelegate(string a)

    then we write the instance of this type

    public Mydelegate _myDelegate;

   then we can use it;

 

Program.cs

 Application.Run(new Form3());

 

Form3.cs

 

 Form2 frm2;
        public Form3()
        {
            InitializeComponent();
            frm2 = Form2.Instance();
          
            if (frm2._FrmMyDelegateString == null)
                frm2._FrmMyDelegateString += fillText;
        }
        private void fillText(string str)
        {
            this.textBox1.Text = str;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frm2.Show();
        }

Form2.cs

private  Form2()
        {
            InitializeComponent();
            myFrm = Form1.Instance();
          
        }
        private static Form2 frm2;
        public static Form2 Instance()
        {
            if (frm2 == null)
                frm2 = new Form2();
            return frm2;
        }
        Form1 myFrm;
        public MyDelegateString _FrmMyDelegateString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (myFrm._myDelegateString == null)
                myFrm._myDelegateString += _FrmMyDelegateString;
            Form1 myfrm1 = Form1.Instance();
            myfrm1.Show();
        }

 

Form1.cs

as the resouces

private Form1()
        {
            InitializeComponent();
        }
        private static Form1 frm;
        public static Form1 Instance()
        {
            if (frm == null)
                frm = new Form1();
            return frm;
        }       
   
        public MyDelegateString _myDelegateString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (_myDelegateString != null)
                _myDelegateString("Proxy Event");
          
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值