c# 窗体间传值

效果

image

代码

  public partial class Form1 : Form
  {
      public Action<string> toform2;//委托
      public Form1()
      {
          InitializeComponent();
          Form2 form2 = new Form2();
          form2.upDataToForm1 += updata;
          this.toform2 += form2.fromform1;
          form2.Show();
      }
      private void updata(string st)
      {
          this.label1.Text = st;
      }
      private void textBox1_TextChanged(object sender, EventArgs e)
      {
          toform2(textBox1.Text);
      }
  }
 public partial class Form2 : Form
 {
     public Action<string> upDataToForm1;//委托
     public Form2()
     {
         InitializeComponent();
     }
     private void textBox1_TextChanged(object sender, EventArgs e)
     {
         upDataToForm1(textBox1.Text);
     }
     public void fromform1(string st)
     {
         this.label3.Text = st;
     }
 }
posted @ 2025-06-09 09:14  阿拉斯加王  阅读(8)  评论(0)    收藏  举报