0

Good morning experts,

I'm trying to save a dynamic forms into files, Forms have no property related to save.

If I use the following code:

// Create a new instance of the form.
            Form form1 = new Form();
            // Create two buttons to use as the accept and cancel buttons.
            Button button1 = new Button();
            Button button2 = new Button();

            // Set the text of button1 to "OK".
            button1.Text = "OK";
            // Set the position of the button on the form.
            button1.Location = new Point(10, 10);
            // Set the text of button2 to "Cancel".
            button2.Text = "Cancel";
            // Set the position of the button based on the location of button1.
            button2.Location
               = new Point(button1.Left, button1.Height + button1.Top + 10);
            // Set the caption bar text of the form.   
            form1.Text = "My Dialog Box";
            // Display a help button on the form.
            form1.HelpButton = true;

            // Define the border style of the form to a dialog box.
            form1.FormBorderStyle = FormBorderStyle.FixedDialog;
            // Set the MaximizeBox to false to remove the maximize box.
            form1.MaximizeBox = false;
            // Set the MinimizeBox to false to remove the minimize box.
            form1.MinimizeBox = false;
            // Set the accept button of the form to button1.
            form1.AcceptButton = button1;
            // Set the cancel button of the form to button2.
            form1.CancelButton = button2;
            // Set the start position of the form to the center of the screen.
            form1.StartPosition = FormStartPosition.CenterScreen;

            // Add button1 to the form.
            form1.Controls.Add(button1);
            // Add button2 to the form.
            form1.Controls.Add(button2);

How can I save it to form1.cs and form1.designer.cs? I want to save those files so I can use them on other projects.

  • Possible duplicate of [Save/Load inputs on C# windows form?](https://stackoverflow.com/questions/7063850/save-load-inputs-on-c-sharp-windows-form) – gunr2171 Oct 22 '18 at 20:10
  • If i understand your question correctly, I'd prefer to create model which include required properties then we can serialize and save serialized model as byte array. –  Oct 22 '18 at 21:22
  • no, my code creates forms based on some user input, the form created needs to be saved as a form, with the designer.cs and the form.cs, so later on I can used it on another project, basically it gives the user some power on how they want to see their form, when they click save form, the form must be saved on a folder, which later I must grab and put the logic to it – Alfredo Matute Oct 22 '18 at 22:23
  • 1
    @AlfredoMatute I think what you're trying to do is quite complex task. Maybe you should look at FluentSharp: https://github.com/fluentsharp/FluentSharp/blob/master/README.md Another way is implementing custom layout engine. https://learn.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-implement-a-custom-layout-engine –  Oct 22 '18 at 23:05
  • what is the point of comments in format `// Set the text of button1 to "OK".` it is pretty obvious that the next line does even without such comment – ASh Oct 23 '18 at 07:39

1 Answers1

1

Use the Application Settings that is already provided:

Properties.Settings.Default.FormText = "Some Value"; //this is a string value, for checkboxes it can be boolean , ...
Properties.Settings.Default.Save(); 

you can find more info about it in here.

You may also want to save your settings in to a text file in some custom format, or just a serialized json format, but I recommend the first option.

Edit:

Considering your comment, a form is compiled in a .exe file and then can be used, saving a .cs file that is a text format, you won't be able to use it, however, you have option of creating Windows Form Control, the it can be compiled to a dll library that you can use it in your other projects.

You should try creating a Windows Form Control Library.

Here is a nice example of Windows Form Control Library: https://www.mindstick.com/Articles/51/creating-and-using-windows-forms-control-library-in-c-sharp-dot-net

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171