在C#中使用委托

要求如下:
	在窗体Form1、Form2中各有一个文本框、一个按钮,现在我需要点击Form1中的按钮,弹出窗体Form2,然后在窗体Form2中的文本框中输入字符串,然后点击Form2中的按钮,将文本框中的值传到Form1中的文本框中。

【第一种方法:】
窗体Form2中的代码如下:
        public delegate void SendDate(object sender);

        public SendDate sendDate;

        private void button1_Click(object sender, EventArgs e)
        {
            if (sendDate != null) {
                sendDate(rtxtValue.Text);//注意:在这里还可以传入一个控件,比如:sendDate(rtxtValue)
            }
            this.Close();
        }

窗体Form1中的代码如下:
	 private void button1_Click(object sender, EventArgs e)
         {
            Form4 f = new Form4();
            f.sendDate = new Form4.SendDate(Funcation);
            f.ShowDialog();
         }


         private void Funcation(object sender)
         {
            this.textBox1.Text = sender.ToString();
	   //this.textBox1.Text=((TextBox)sender).Text;
         }


【第二种方法:】
Form2:
	public delegate void SendMsg(string msg);
	private void button1_Click(object sender, EventArgs e)
       	 {
            		Form1 f = new Form1();
            		SendMsg sm = new SendMsg(f.getMsg);
           		sm(textBox1.Text);
            		this.Close();
	}
Form1:
	   public void getMsg(string msg)
     	   {         
         		   MessageBox.Show(msg);
  	     }

      	  private void button1_Click(object sender, EventArgs e)
      	  {
      	      Form2 f = new Form2();       
     	       f.Show();
    	    }

 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值