在两个窗口窗体之间传递值

发布于 2024-12-16 21:50:03 字数 275 浏览 0 评论 0原文

我有两个窗口表单,即form1和form2。在form1中,用户必须输入一些值。此页面中有下一个按钮。通过单击下一个按钮,form2被打开,我隐藏表单1。在Form2中,也有一些输入字段.这里我使用构造函数方法访问form1的一些值

在任何情况下,如果form 1中输入的值是错误的,用户单击form2中的后退按钮并转到form1,修改值并单击下一步转到form2。

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

请建议。

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

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.

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.

Please suggest.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

沫尐诺 2024-12-23 21:50:04

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

当存在常规值时,这将起作用。

FORM 1:

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2(textBox1.Text, "1001");
    f2.Show();
}

FORM 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.

弱骨蛰伏 2024-12-23 21:50:04

只要清除form2的值,然后在修改form1时重新加载即可,就可以得到新的值。

Just clear the values of form2 and reload when they modify form1.Then you may get the new values.

相对绾红妆 2024-12-23 21:50:04

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

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.

暮色兮凉城 2024-12-23 21:50:04

在 Windows 窗体中传递值的最简单方法是仅在一种窗体中创建会话变量并在第二种窗体中使用它们。

The Easiest way of passing values in windows forms is to just create Session variables in one form and use them in second form.

乖乖公主 2024-12-23 21:50:04

这是在窗口窗体之间传递值的简单解决方案。以下示例从 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

Here is a simple solution to pass values between windows forms. The following example takes a value from a Form1 textBox and 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.

    //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

吻安 2024-12-23 21:50:03

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

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();
    }

}

那样显示 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();
    }
}

在 Form1 的按钮单击中,您应该只需要验证输入的数据并像Sidenote
听起来很像您正在尝试构建某种向导功能。我建议您在谷歌上搜索一下,因为可能已经存在一些可以帮助您的预定义控件:http://www.google.com/search?hl=en&sourceid=chrome&ie=UTF-8&q=wizard+winforms

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

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 wizard functionality. 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文