How to not lose data what switching between forms in C#

49 Views Asked by At

I am currently trying to have a pop up form and when I click a button it pops up. When it does you enter your information then send it back.

Note there might be some errors because it is not copy and paste. I only included the code that matters

Form1:

namespace estController
{
public partial class MainForm : Form
{
    string Tempcustomer = "";
    string TempManager = "";
    string TempManagerId = "";
    string TempName = "";
    string TempProjectId = "";
    string TempSharePointID = "";
    string Tempstatus = "";


    private void buttonBegin_Click(object sender, EventArgs e)
    {
        if(ManualEnterNumber.Checked)
        {
            buttonBegin.Enabled = false;
            Manual_Input F2 = new Manual_Input();

            F2.Show();

        }
    }

    private void buttonNext_Click(object sender, EventArgs e)
    {
         MessageBox.Show("test" + TempName);
    }

    internal void receiveData(string boxCustomer, string boxManager, string boxManagerId, string      boxName, string boxProjectId, string boxSharePointId, string boxStatus)
    {
        Tempcustomer = boxCustomer;
        TempManager = boxManager;
        TempManagerId = boxManagerId;
        TempName = boxName;
        TempProjectId = boxProjectId;
        TempSharePointID = boxSharePointId;
        Tempstatus = boxStatus;
    }
}
}

Form 2:

namespace estController
{
public partial class Manual_Input : Form
{

    private void buttonEnter_Click(object sender, EventArgs e)
    {
        MainForm Form = new MainForm();

        Form.receiveData(boxCustomer.Text, boxManager.Text, boxManagerId.Text, boxName.Text, boxProjectId.Text, boxSharePointId.Text, boxStatus.Text);
        

        this.Close();
    }
}
}

What I expect to happen is to click the begin button. When that happen form 2 opens and I enter the data needed. Next I hit the button to send the data then close the form. The data should be saved in the variables that are initialized at the beginning of form1. Finally I can click the next button and check the data is there.

When stepping through the code I can see that data being assigned but when I then hit the next button the data is gone.

2

There are 2 best solutions below

1
David On

This is creating a new instance of MainForm:

MainForm Form = new MainForm();

That instance is not the same as the instance you just came from. Consider an analogy:

A car rolls off an assembly line. You open the trunk of the car and put something in it. Moments later, another car rolls off the assembly line. This car is identical to the first car in every way. When you open the trunk on the second car, do you expect to see what you placed in the trunk in the first car?

You can pass a reference to the object when creating an instance of Manual_Input. For example, add this constructor to Manual_Input:

private MainForm _mainForm;

public Manual_Input(MainForm mainForm)
{
    InitializeComponent();
    _mainForm = mainForm;
}

This will require that newly created instances of Manual_Input need a reference to a MainForm, since they internally depend on one to perform their tasks. Pass that reference when creating the instance:

Manual_Input F2 = new Manual_Input(this);
F2.Show();

Then you can use that reference when performing the logic in Manual_Input:

private void buttonEnter_Click(object sender, EventArgs e)
{
    _mainForm.receiveData(boxCustomer.Text, boxManager.Text, boxManagerId.Text, boxName.Text, boxProjectId.Text, boxSharePointId.Text, boxStatus.Text);
    this.Close();
}
0
Ross Bush On

This is not a direct answer, however, there may be cleaner ways to share common resources and reference dependencies.

Project.cs

namespace WinFormsApp2
{
    public class Project
    {
        public string Tempcustomer { get; set; }
        public string TempManager { get; set; }
        public string TempManagerId { get; set; }
        public string TempName { get; set; }
        public string TempProjectId { get; set; }
        public string TempSharePointID { get; set; }
        public string Tempstatus { get; set; }
    }
}

Form1.cs

namespace WinFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var project = Form2.EditProject();
            if(project != null)
            {
                MessageBox.Show("Project Edited");
            }
            else
            {
                MessageBox.Show("Project Not Edited");
            }
        }
    }

    
}

Form2.cs

namespace WinFormsApp2
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }        

        private void btnOK(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnCancel(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            this.Close();
        }
                  
        public static Project? EditProject()
        {                
            if(new Form2.ShowDialog() != DialogResult.OK);
               return null;

            return new Project
            {
                Tempcustomer = "Hey!"// txtCustomer.Text;
            };            
        }
    }
}