0% found this document useful (0 votes)
25 views2 pages

Retaining Data On Multiple Forms

This document provides code to allow passing data between forms in a Windows Forms application without creating new instances of the forms each time they are opened. It demonstrates: 1. Declaring static variables to reference each form type 2. Creating the instances of each form only once and reusing them 3. Checking a boolean flag to determine if a form needs to be newly created or can be shown directly 4. Passing data between forms by accessing the static form references This allows opening multiple forms while maintaining the data entered on each form across launches.

Uploaded by

Sam Ainsworth
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Retaining Data On Multiple Forms

This document provides code to allow passing data between forms in a Windows Forms application without creating new instances of the forms each time they are opened. It demonstrates: 1. Declaring static variables to reference each form type 2. Creating the instances of each form only once and reusing them 3. Checking a boolean flag to determine if a form needs to be newly created or can be shown directly 4. Passing data between forms by accessing the static form references This allows opening multiple forms while maintaining the data entered on each form across launches.

Uploaded by

Sam Ainsworth
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

So far when we have used multiple forms we have not needed to keep the data on the calling form.

Each time we call formname = new Form1 we create a new instance of the form and all previous data is lost. This is how to prevent this happening 1. Create a project with two forms From1 & form2. 2. Place a button and a text box on each form. Go to you first form the form that will display when the project starts. 1. Type the last three lines of the code below.
public partial class Form1 : Form { //sets up forms to use public static Form2 Frm2; public static Form1 frm1; bool created = false;

This sets up two variables one of type Form1 and one of type Form2. The last line sets a Boolean variable which lets us know if the form has been created. This needs to be set to false at the start.
2.

Under the

InitializeComponent();

type the following code

//creates instance of other forms Frm2 = new Form2(); Frm2.Visible = false; frm1 = this;

3. Double click on the button. In the buttonclick procedure type the following
private void button1_Click(object sender, EventArgs e) { //call other form this.Hide(); //disables the form you are currently on if (created == false) //Check if the second form has been previously used. { //create second form for the first time Frm2 = new Form2(); Frm2.Show(); created =true; //set boolean variable to true - notes that form has been created } else { Frm2.Show(); //form has already been created so just show it. } }

4. Go to your Form2 and double click on the button. following code. private void button1_Click(object sender, EventArgs e) { Form1.frm1.Show(); Form1.Frm2.Visible = false; }

Simply add the

This simply returns you to your original form. Run the program, add some text to the text box on form1, open form 2 text that form1 disappears. Add some text to the textbox on form2. Return to form1 1. Does form2 disappear. 2. Does form1 appear with the same text in the textbox. 3. Reopen form1 is the text still in the textbox.

You might also like