C#窗体间传值

窗体间传值(From2Button事件将From2中的textbox1text值传给From1lable

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;

 

            ((Label)frm1.Controls["label1"]).Text = this.textBox1.Text;

            this.Close(); 
  }  
  }  

 

 

 

 

或者

 

 

public partial class Form1 : Form  
  {  
  private void button1_Click(object sender, EventArgs e)  
  {  
  Form2 frm2 = new Form2(this);  
  frm2.Show();  
  }  
  }  

 

 
在窗体form2的窗体类中声明变量,并改写初始化构造函数如下:

 

public partial class Form2 : Form  
  {   
 
form1 form1;
public form2(form1 f1)
{
InitializeComponent();
form1 = f1;
}
private void button1_Click(object sender, EventArgs e)  
  {   
         
     form1.lable1.text = this.textBox2.text;
  }  
  }  

 

 

你可能感兴趣的:(C#,入门系列,c#,button,object,class,textbox)