在两个Windows窗体之间传递值
本文关键字:之间 窗体 Windows 两个 | 更新日期: 2023-09-27 18:15:20
我有两个窗体,窗体1和窗体2。在form1中,用户必须输入一些值。在这个页面上有下一个按钮。通过点击下一个按钮form2打开,我隐藏表单1。在Form2中也有一些输入字段。这里我通过使用构造函数方法
访问form1的一些值在任何情况下,如果表单1中输入的值有误,用户点击form2中的后退按钮,进入form1,修改值,点击下一步,进入form2。
问题是,当我第二次修改form1中的值并单击下一步转到form2时,我得到了form1的旧值。
请建议。
在两个表单之间传递数据可以通过不同的方式完成,但可能最简单的一种方式是使用一个对象来保存两个表单的数据和公共属性,并使用该对象来设置数据。例如:
public class MyDataObj
{
public string Firstname { get; set; }
public string Surname { get; set; }
}
然后在你实例化表单的"Program"中,你有像
这样的东西public class Program
{
public static voic Main()
{
MyDataObj myObj = new MyDataObj();
Form1 f1 = new Form1();
f1.DataObj = myObj;
f1.Show();
}
}
在Form1的按钮点击中,你只需要验证输入的数据并显示Form2,如
public class Form2
{
public MyDataObj DataObj { get; set; } //obj shared by both forms
void btnNext_Click(...)
{
//validate the input and set it on DataObj
Form2 f2 = new Form2(); //Note: instead of always re-instantiating the form you may want to have it somewhere already prepared and just show it here
f2.DataObj = DataObj; //pass the data object to second form
f2.Show();
}
}
旁注
这听起来很像您试图构建某种向导功能。我建议你谷歌一下,因为可能已经存在一些预定义的控件来帮助你:http://www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+winforms
FORM 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(textBox1.Text, "1001");
f2.Show();
}
形式2:
public partial class Form2 : Form
{
string sname;
string sID;
public Form2(string name, string id, Form a)
{
sname = name;
sID = id;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = sname + " ID" + sID;
}
}
只需清除form2的值,并在它们修改form1时重新加载。
您可能想要创建一个类来保存表单之间共享的值。当单击form1上的next按钮时,应该验证输入—如果发现错误,不要让用户进入下一个屏幕。如果没有发现错误,将值保存到一个类中,并通过构造函数将其传递给form2。当form2加载时,让它从值中填充控件。
在windows窗体中传递值的最简单方法是在一种窗体中创建Session变量,然后在另一种窗体中使用它们。
这是一个在windows窗体之间传递值的简单解决方案。下面的示例从Form1 textBox获取一个值,并且将该值作为字符串值发送给Form2标签(使用Visual Studio 2012 Windows窗体应用程序编写的示例)。如果去掉注释,这里的代码就不多了。
//Begin Form1 Code
//Delcalre a string to be used by Form1 for a value eventually returned from Form2
private string returnedValue;
//Declare a string that can be accessed outside of Form1 by Form2
public string returnValue
{
get { return returnedValue; }
set { returnedValue = value; }
}
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
void Form1_Load(object sender, EventArgs e)
{
/*
Form1_Load can be used to loop back and forth from Form1 to Form2
which can be useful when writing spiders for custom search engines
but this type of functionality is not needed for this example.
*/
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide(); //This will hide Form1, could also use this.Close();
Form2 f2 = new Form2(); //Declare instance of Form2
f2.passValue = textBox1.Text; //Grab text from textBox1 and pass to Form2!
f2.Show(); //Run Form2 with passed value now available!
}
//End Form1 Code
//Begin Form2 Code
//Please excuse the naming convention :-)
//The super long string Ids are intended to be helpful
private string valueFromForm1ToBeUsedInForm2;
public string passedValueFromForm1
{
get {return valueFromForm1ToBeUsedInForm2;}
set { valueFromForm1ToBeUsedInForm2 = value; }
}
public Form2()
{
InitializeComponent();
this.Load += Form2_Load;
}
void Form2_Load(object sender, EventArgs e)
{
label1.Text = valueFromForm1ToBeUsedInForm2;
Form1 f1 = new Form1(); //Declare a new instance of Form1
//The following would send back to Form1 the value previously sent from Form1
f1.returnValue = valueFromForm1ToBeUsedInForm2;
//or you could send back a new value to Form1 by commenting out above f1.returnValue
//and uncomment below
//f1.returnValue = "maybe a value derived using original value sent by Form1";
// The following will redirect back to Form1 and close Form2 automatically
// You may want to handle this with a button event if not building a spider
// or custom search engine
f1.Show();
this.Close();
}
//End Form2
credit:以上部分来源于https://www.youtube.com/watch?v=PI3ad-TebP0