Question

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.

No correct solution

OTHER TIPS

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

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.

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

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.

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top