将值从一种forms传递给另一种forms

我有两个名为form1form2窗体:

  • form1由一个label和一个button
  • form2是由一个textBox和一个button

当我点击form1button,这将显示form2 。 一旦我按下form2的button, form1.label任何input都应该写回到form1.label
我有下面的代码,但它不起作用。

 // Code from Form 1 public partial class Form1 : Form { public void PassValue(string strValue) { label1.Text = strValue; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 objForm2 = new Form2(); objForm2.Show(); } } // Code From Form 2 public partial class Form2 : Form { public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form1 objForm1 = new Form1(); objForm1.PassValue(textBox1.Text); this.Close(); } } 

和一个截图:

应用截图

我怎么能意识到这一点?

您不访问您创buildform2的form1。 在form2 button1_Click中,创buildForm1的新实例,与初始化不一样。 您可以将您的form1实例传递给form2构造函数,如下所示:

  // Code from Form 1 public partial class Form1 : Form { public void PassValue(string strValue) { label1.Text = strValue; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 objForm2 = new Form2(this); objForm2.Show(); } } // Code From Form 2 public partial class Form2 : Form { Form1 ownerForm = null; public Form2(Form1 ownerForm) { InitializeComponent(); this.ownerForm = ownerForm; } private void button1_Click(object sender, EventArgs e) { this.ownerForm.PassValue(textBox1.Text); this.Close(); } } 

就像在其他文章中提到的那样,您将无法通过创buildForm1的新实例来引用原始Form1。 您可以将Form1传递给Form2的构造函数或将Form2的文本作为公共属性公开,但我通常更喜欢使用代理来保持松散耦合。

 // Code from Form 1 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 objForm2 = new Form2(); objForm2.PassValue += new PassValueHandler(objForm2_PassValue); objForm2.Show(); } public void objForm2_PassValue(string strValue) { label1.Text = strValue; } } // Code From Form 2 public delegate void PassValueHandler(string strValue); public partial class Form2 : Form { public event PassValueHandler PassValue; public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (PassValue != null) { PassValue(textBox1.Text); } this.Close(); } } 

当你在做:

 Form1 objForm1 = new Form1(); objForm1.PassValue(textBox1.Text); 

…您正在创build一个新的Form1并调用错误的Form1对象上的PassValue方法。 相反,你可以这样做:

 public partial class Form1 : Form { // This is the text that will be entered in form2 public String form2text; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // Show form2 Form2 objForm2 = new Form2(this); objForm2.ShowDialog(); // When form2 is closed, update the label text on form1 label1.Text = form2text; } } public partial class Form2 : Form { // This is the instance of Form1 that called form2 private Form1 form1caller; public Form2(Form1 form1caller) { InitializeComponent(); this.form1caller = form1caller; } private void button1_Click(object sender, EventArgs e) { // Pass the textBox value to form1 before closing form2 form1caller.form2text = textBox1.Text; this.Close(); } } 

我只是试过这个代码,它的工作原理,肯定会帮助你。

在第一种forms( Form1 )types如下:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 f = new Form2(textBox1.Text); f.ShowDialog(); } } 

在第二种forms( Form2 )中使用以下代码:

 public partial class Form2 : Form { public Form2( string st) { InitializeComponent(); textBox1.Text = st; } private void Form2_Load(object sender, EventArgs e) { } } 

你可以这样做:

 class Form2 { public string ReturnedText = ""; private void button1_Click(object sender, EventArgs e) { ReturnedText = textbox1.Text; Close(); } } 

并在form1中

 Form2 objForm2 = new Form2(); objForm2.ShowDialog(); string ret = objForm2.ReturnedText; 

您应该将form1上的引用传递给form2,而不是在此代码中创build新的实例:

 private void button1_Click(object sender, EventArgs e) { Form1 objForm1 = new Form1(); // ← this is another form1, not that you see objForm1.PassValue(textBox1.Text); this.Close(); } 

我通常的做法是这样的:

我在Form2类上放置一个公共属性:

 public string ValueFromForm1 { get; set; } //In the constructor, or other relevant method, I use the value public Form2() { form2LabelToDisplayForm1Value.Text = ValueFromForm1; } 

为了将某些内容返回给Form1,您需要将一个公共属性添加到Form1类以接收该值,然后将对Form2的引用发送到Form2,以便Form2可以设置值:

 //Add reference property to Form2 class public Form1 CallingForm { get; set; } //Form2 can access the value on Form1 as follows: private someMethod() { this.CallingForm.ValueFromForm2 = "Info coming from form 2"; } 

然后

 //Add public property to Form1 class public string ValueFromForm2 { get; set; } //When Form2 is created, set the reference property Form2 objForm2 = new Form2(); objForm2.CallingForm = this; objForm2.Show(); 

由于Form2现在具有对创build的Form1的引用,因此无需在Form2中的任何位置调用新的Form1()。 所有Form2所要做的就是在引用上设置值,然后closures它自己。

这就是你要做的事情:

 // Code from Form 1 public partial class Form1 : Form { public string MyValue { get; set; } public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 objForm2 = new Form2(); objForm2.textBox1.Text = MyValue; objForm2.MainForm = this; objForm2.Show(); } } // Code From Form 2 public partial class Form2 : Form { public Form1 MainForm { get; set; } public Form2() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MainForm.MyValue = textBox1.Text; MainForm.Show(); this.Close(); } } 

表格1代码…: –

 namespace Passing_values_from_one_form_to_other { public partial class Form1 : Form { string str; private String value1;//taking values from form no _of_test_cases public string value { get { return value1; } set { value1 = value; } } public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { textBox1.Text = str; } private void button1_Click(object sender, EventArgs e) { Form2 f2 = new Form2(); f2.ShowDialog(); str = f2.passvalue; } } } 

表格2代码….: –

 namespace Passing_values_from_one_form_to_other { public partial class Form2 : Form { private string str; public string passvalue { get { return str; } set { str = value; } } public Form2() { InitializeComponent(); } private void Btn_Ok1_Click(object sender, EventArgs e) { passvalue = textBox1.Text; this.Close(); } } } 

直接执行它你会得到清晰的图片….同样的方式,你可以将值从一个窗体传递给其他…

如果您遇到任何问题,请发表您的评论…

希望这将有助于…

否则你可以参考这个video…

http://www.youtube.com/watch?v=PI3ad-TebP0