Reputation:
I have a very complicated windows application which has 8 forms. There are "Previous" and "Next" buttons on each form to back and forth for easily editing forms.
private void ShowNext()
{
if (FormNext == null)
FormNext = new SomeForm(this);
FormNext.Show();
this.Hide();
}
private void ShowPrev()
{
FormPrev.Show();
this.Hide();
}
private void btnNext_Click(object sender, EventArgs e)
{
ShowNext();
}
private void btnBack_Click(object sender, EventArgs e)
{
ShowPrev();
}
Each form will execute different sql insert table command. But the question is that I don't want to finish all insertion on each form.
What I want to is to finish all in the last form once the user confirms the correct input values. That means I have to pass all variables from the very beginning to the last one.
Question: can the memory hold all variables? How to pass them cross forms?
Thanks.
Upvotes: 0
Views: 3218
Reputation: 223402
Define a static public field in your main form, and update the value in each sub forms. Finally select the field in your last form for insertion
Something on the following lines:
public partial class Form1 : Form //your main fomr
{
public static List<MyContainerClass> myContainer= new List<MyContainerClass>();
.............
Here MyContainerClass could be some class defined in your code, which can hold all the values you want to persist. If its just a string, then you may create a list of string. Then in each form set value
Form1.myContainer.Add(new_values);
In the last form you can access the values
foreach(MyContainerClass mc in Form1.myContainer)
{
//Do your work
}
Upvotes: 1
Reputation: 69
You can pass your string queries easily to your new/old form's public variable like below
protected void btnNext_Click(object sender, EventArgs e)
{
MyForm2 x = new MyForm2();
x.Query = "My Query"; // here "Query" is your custom public string variable on form2
x.Show()
}
Upvotes: 1
Reputation: 41256
You just need to hold a reference to some context that has access to the data you need.
protected void btnNext_Click(object sender, EventArgs e)
{
SomeContext.Push(myFormStateDataThatIsReady);
ShowNext();
}
Then, when the final step comes around, you can pull this data off that context and write that data.
Upvotes: 0
Reputation: 3804
Create a static singleton class that will hold all your variables.
Upvotes: 0