windows 在两个窗体之间传递值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8208421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
passing values between two windows form
提问by Shipu
I have two windows form say form1 and form2.In form1 user have to input some values.There is next button in this page .By clicking on next button form2 get opened and i am hide form 1.In Form2 also there are some input fields.here i am accessing some values of form1 by using constructor method
我有两个窗体,比如 form1 和 form2。在 form1 用户必须输入一些值。这个页面中有下一个按钮。通过点击下一个按钮 form2 被打开,我隐藏了表单 1。在 Form2 中还有一些输入字段.这里我使用构造函数方法访问 form1 的一些值
In any situation if the values enetered in form 1 is wrong user click the back button in form2 and go to form1,Modify the values and click next to come to form2.
在任何情况下,如果表单1中输入的值是错误的用户单击form2中的后退按钮并转到form1,修改值并单击下一步来到form2。
The problem is that when second time i modify the values in form1 and click next to go to form2,there i am getting old values of form1.
问题是,当我第二次修改 form1 中的值并单击下一步转到 form2 时,我得到了 form1 的旧值。
Please suggest.
请建议。
回答by Juri
Passing data between two forms can be done in different ways but probably the simplest one is to have an Object that holds the data and public properties on both forms that use that object for setting the data. For example:
在两个表单之间传递数据可以通过不同的方式完成,但最简单的方法可能是拥有一个对象来保存数据和使用该对象设置数据的两个表单上的公共属性。例如:
public class MyDataObj
{
public string Firstname { get; set; }
public string Surname { get; set; }
}
Then in your "Program" where you instantiate the form, you have something like
然后在您实例化表单的“程序”中,您有类似的东西
public class Program
{
public static voic Main()
{
MyDataObj myObj = new MyDataObj();
Form1 f1 = new Form1();
f1.DataObj = myObj;
f1.Show();
}
}
In the button click of Form1 you should just have to validate the entered data and to show Form2 like
在 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();
}
}
Sidenote
It very much sounds like you're trying to build some kind of wizardfunctionality. I'd suggest you to google a bit as there might exist already some predefined controls that help you: http://www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+winforms
旁注
听起来很像您正在尝试构建某种向导功能。我建议你谷歌一下,因为可能已经存在一些可以帮助你的预定义控件:http: //www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+ winforms
回答by Madhusudhan
FORM 1:
表格 1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(textBox1.Text, "1001");
f2.Show();
}
FORM 2:
表格 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;
}
}
This will work when there is regular values.
当有常规值时,这将起作用。
回答by prema
Just clear the values of form2 and reload when they modify form1.Then you may get the new values.
只需清除form2的值并在他们修改form1时重新加载。然后您可能会得到新的值。
回答by Jason
you probably want to create a class that holds the values that are shared between the forms. when the next button on form1 is clicked, the input should be validated - if errors are found, don't let the user advance to the next screen. if no errors are found, save the values to a class and pass it to form2, via the constructor. when form2 loads, have it populate it's controls from the values.
您可能想要创建一个类来保存表单之间共享的值。当单击 form1 上的下一个按钮时,应验证输入 - 如果发现错误,不要让用户前进到下一个屏幕。如果没有发现错误,则将值保存到一个类中并通过构造函数将其传递给 form2。当 form2 加载时,让它从值填充它的控件。
回答by Sagar
The Easiest way of passing values in windows forms is to just create Session variables in one form and use them in second form.
在 Windows 窗体中传递值的最简单方法是以一种形式创建会话变量并在第二种形式中使用它们。
回答by user3777549
Here is a simple solution to pass values between windows forms. The following example takes a value from a Form1 textBoxand sends the value to a Form2 label as a string value(Example written using Visual Studio 2012 Windows Form Application). There really isn't much code here if you remove the comments.
这是在 Windows 窗体之间传递值的简单解决方案。下面的示例从 Form1 文本框中获取一个值,并将该值作为字符串值发送到 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
Credits: The above was derived in part from the following, https://www.youtube.com/watch?v=PI3ad-TebP0
致谢:以上部分源自以下内容,https://www.youtube.com/watch?v=PI3ad-TebP0