在两个窗口之间传递值

时间:2011-11-21 07:17:15

标签: c# .net windows

我有两个窗口形式说form1和form2.In form1用户必须输入一些值。这个页面中有下一个按钮。点击下一个按钮form2打开,我隐藏形式1.在Form2中也有一些输入字段。我通过使用构造函数方法

访问form1的某些值

在任何情况下,如果表单1中的值都是错误的,请单击form2中的后退按钮,然后转到form1,修改值,然后单击“下一步”进入form2。

问题是当我第二次修改form1中的值并单击下一步转到form2时,我得到了form1的旧值。

请建议。

6 个答案:

答案 0 :(得分:2)

在两个表单之间传递数据可以用不同的方式完成,但最简单的方法是使用一个Object来保存使用该对象设置数据的两个表单上的数据和公共属性。例如:

public class MyDataObj
{
    public string Firstname { get; set; }
    public string Surname { get; set; }
}

然后在您实例化表单的“程序”中,您有类似

的内容
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

答案 1 :(得分:2)

表格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;
    }
}

当有常规值时,这将有效。

答案 2 :(得分:0)

只需清除form2的值并在修改form1时重新加载。然后你就可以得到新值。

答案 3 :(得分:0)

您可能想要创建一个包含表单之间共享值的类。当单击form1上的下一个按钮时,应验证输入 - 如果发现错误,则不要让用户前进到下一个屏幕。如果没有找到错误,请将值保存到类中,并通过构造函数将其传递给form2。当form2加载时,让它从值中填充它的控件。

答案 4 :(得分:0)

在Windows窗体中传递值的最简单方法是在一个窗体中创建Session变量,并以第二种形式使用它们。

答案 5 :(得分:0)

这是在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

致谢:以上内容部分来自以下https://www.youtube.com/watch?v=PI3ad-TebP0

相关问题