通过公共静态类进行传值; 
通过绑定事件进行传值; 
使用Attribute 
public partial class Form1 : Form 
  { 
  private void button1_Click(object sender, EventArgs e) 
  { 
  Form2 frm2 = new Form2(); 
  frm2.Show(this); 
  } 
  } 
  public partial class Form2 : Form 
  { 
  private void button1_Click(object sender, EventArgs e) 
  { 
  Form1 frm1 = (Form1)this.Owner; 
  ((TextBox)frm1.Controls["textBox1"]).Text = this.textBox2.Text; 
  this.Close(); 
  } 
  } 
还可使用委托
使用委托实现两个窗体的交互:
// 主窗体中
FromB frm = new FromB("Hello A");
frm.onReportProgress = new DoReportProgress(OnReportProgress);
frm.ShowDialog(); // 显示窗体
private void OnReportProgress(string str)
{
  MessageBox.Show(str);
}
// 子窗体
public delegate void DoReportProgress(string strInfor);
public DoReportProgress onReportProgress;
public FromB(string str) // 传入父窗体的值
{
  InitializeComponent();
  MessageBox.Show(str);
}
public void button1()
{
  if(onReportProgress!=null)
  onReportProgress("Hello B"); // 调用委托将值返回给父窗体
}

相关文章:

  • 2022-02-23
  • 2022-03-08
猜你喜欢
  • 2022-12-23
  • 2022-01-09
  • 2022-12-23
  • 2021-04-11
  • 2021-08-26
  • 2021-10-08
相关资源
相似解决方案