编程语言
首页 > 编程语言> > C#窗体间互相传值

C#窗体间互相传值

作者:互联网

Demo窗体图片,Form1

 

Demo窗体图片,Form2

公共委托

using System;

namespace _DeleFrm
{
  public class Dele
  {
    public static Action<string> SendDele;
  }
}

form1窗体代码

 

using System;
using System.Windows.Forms;

 

namespace _DeleFrm
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
      Dele.SendDele = Receive;//委托
    }

 

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

 

   /// <summary>
   /// 接收数据方法
   /// </summary>
   /// <param name="Msg"></param>
   private void Receive(string Msg)
   {
    textBox1.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss ")+Msg+"\r\n");
   }
  }
}

 

 form2窗体代码

using System;

using System.Windows.Forms;

namespace _DeleFrm
{
  public partial class Form2 : Form
  {
    public Form2()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      Send(textBox1.Text);
    }
    /// <summary>
    /// 信息发送
    /// </summary>
    /// <param name="a"></param>
    private void Send(string a)
    {
      Dele.SendDele(a);
    }
  }
}

标签:C#,System,private,Form2,窗体,using,public,传值
来源: https://www.cnblogs.com/Tian-HW/p/15150635.html