0

I am working on a multi form project in C# and I am having issues with the forms data persisting when changing between tabs. Each tab will open the respective form as shown below.

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

        private void Form1_Load(object sender, EventArgs e)
        {
            loadForm(new FormHome());
        }

        #region FORM CALLER
        public void loadForm(object Form)
        {
            if (this.gunaPanelHome.Controls.Count > 0)
                this.gunaPanelHome.Controls.RemoveAt(0);
            Form fetched = Form as Form;
            fetched.TopLevel = false;
            fetched.BringToFront();
            fetched.Dock = DockStyle.Fill;
            this.gunaPanelHome.Controls.Add(fetched);
            this.gunaPanelHome.Tag = fetched;
            fetched.Show();
        }

        private void gunaHomeButton_Click(object sender, EventArgs e) => loadForm(new FormHome());
        private void gunaDelayButton_Click(object sender, EventArgs e) => loadForm(new FormDelay());
        private void gunaAssemButton_Click(object sender, EventArgs e) => loadForm(new FormAssembly());
        private void gunaBuildButton_Click(object sender, EventArgs e) => loadForm(new FormBuilder());
        private void gunaSettingsButton_Click(object sender, EventArgs e) => loadForm(new FormSettings());
        #endregion
    }

I have data in other forms that I want to keep throughout the tab changes, and was wondering if there was an easier way besides building a constructor that set data through each action change. Overall, I would like to just .hide() the current form in the window.

  • 1
    What do you want to save all controls in their complete state, or a group, or only certain properties to act as options and preferences? How do you want persistence? Application settings? JSON, XML, INI, TXT, Binary? Encrypted? Compressed? Database, which engine? Windows registry? What are the goal of `void gunaHomeButton_Click(object sender, EventArgs e) => loadForm(new FormHome());` and `loadForm(new FormHome()); in the Form Load event`? What is FormHome? –  Aug 09 '21 at 16:12
  • @OlivierRogier That line is a pointer to load the form into the panel. Each button loads a different form into the panel, but the data doesnt save inside the forms when switching. – DuckyDev Aug 09 '21 at 16:16
  • I finally understand, you implemented a multiview system, is that? I found it unconventional. But what do you want to save in fact? Where? How? Due to weird and unclean nor clear namings I don't really understand loadForm and what the code do nor why you create new forms every click. –  Aug 09 '21 at 16:18
  • @OlivierRogier Yes it is a multiview system. I want to save preferences the user places in each form to be used in the FormBuilder. I want to save the users options for the build they are making with my program. Example - output filename, file password, file version, etc. I dont know WHERE to store, thats why I need assistance – DuckyDev Aug 09 '21 at 16:21
  • In this case you should be able to use the .NET application settings engine to save and restore some properties of some controls like TextBoxes and CheckBoxes if you like: does it fit? Or any JSON/XML... tech. What does fit? –  Aug 09 '21 at 16:22
  • The options that the user sets doesnt have to be saved when the application closes. The user sets parameters and preferences only while using, for the output. Once done they can close and dont need those preferences. What do you think – DuckyDev Aug 09 '21 at 16:23
  • If you only need to keep the state of the controls during the lifetime of the started process and not after, and if the forms are single instances, which I understand they are, just use singleton forms... don't get bored with other stuff. Thus, you will connect the main form viewer container to each static `Instance` property of the singleton of the desired current form of the form class one at a time. It's all and that simple. Does it fit? Otherwise I still didn't understand what you wanted to do. –  Aug 09 '21 at 16:26
  • I dont know singleton forms - The user gives a file, the program reads the file, the user gives a password and file version and can then go to FormBuilder and should be able to output a file with their set preferences. The preferences would be output filename, the contents, a password if they set it. Each time the form is loaded though, it removes the data that the user sets. – DuckyDev Aug 09 '21 at 16:33
  • Does this answer your question? [Can I make a Form a singleton?](https://stackoverflow.com/questions/14313646/can-i-make-a-form-a-singleton) and [open new winform using singleton pattern](https://stackoverflow.com/questions/42082953/open-new-winform-using-singleton-pattern/42083062) and [open new winform using singleton pattern](https://stackoverflow.com/questions/42082953/open-new-winform-using-singleton-pattern) and [How to avoid multiple instances of windows form in c#](https://stackoverflow.com/questions/1403600/how-to-avoid-multiple-instances-of-windows-form-in-c-sharp) –  Aug 09 '21 at 16:38
  • By making your subview forms as singletons, you don't need to instantiate them every time you click a button and you will reuse objects and thus you will keep the states during the lifetime of the process. And if you want to load and save options somewhere, you can of course do that too. –  Aug 09 '21 at 16:41
  • @OlivierRogier that does not answer my questions unfortunately. I need to just show and hide each form. No worries. – DuckyDev Aug 10 '21 at 02:07
  • Making forms as singletons will solve your problem in "less than one minute" in a simple, clean, robust and easy to use way. –  Aug 10 '21 at 11:17
  • Singleton just looks like making a constructor to access other Form page items, I need to not LOSE DATA. – DuckyDev Aug 10 '21 at 13:02
  • A singleton is unique, a single instance, like static class but for non-static, thus usable for forms, and can solve your problem by allowing what you requested: https://refactoring.guru/design-patterns/singleton –  Aug 10 '21 at 13:06
  • I need to just show the panel instead of re-Load it. – DuckyDev Aug 10 '21 at 14:55
  • @DuckyDev, Could you tell me the code you set the persist data? – Jack J Jun Aug 11 '21 at 06:00
  • @JackJJun-MSFT What do you mean? – DuckyDev Aug 11 '21 at 15:31

0 Answers0