Home > database >  One form to another without losing the values to previous entered values in form 1 c#
One form to another without losing the values to previous entered values in form 1 c#

Time:10-29

How do I go back to form 1 without losing the entered values from the user?

I tried:

  Form1 fm1 = new Form1();
  fm1.Show();
  this.Hide();

but I eraeses the values

CodePudding user response:

In your application, create a c# class and make it static. declare static variable there.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    static class Class1
    {
        public static string static_variable = null;
    }
}

Then you can access this variable in both forms.

Class1.static_variable = "AAA";

if you dont understand please let me know. you can pass values using delegates too,but this way is easy than that.

CodePudding user response:

This is what i did in my code with multiple forms:


public Form2 form2;
public Form3 form3;
public Form4 form4;
private Form activeForm = null;

public Form1() {
  InitializeComponent();
  form2 = new Form2();
  form3 = new Form3();
  form4 = new Form4();
}

private void openForm(Form childForm) {
  if (activeForm != null) {
    activeForm.Visible = false;
    activeForm = null;
  }
  activeForm = childForm;
  childForm.TopLevel = false;
  childForm.FormBorderStyle = FormBorderStyle.None;
  childForm.Dock = DockStyle.Fill;
  childForm.Refresh();
  // Container Panel is my parent panel
  containerPanel.Controls.Add(childForm);
  containerPanel.Tag = childForm;
  containerPanel.Refresh();
  childForm.BringToFront();
  childForm.Show();
  childForm.Refresh();
}
  • Related