Alfredo Matute
Alfredo Matute

Reputation: 193

save dynamic created form

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.

Upvotes: 0

Views: 95

Answers (1)

Ashkan Mobayen Khiabani
Ashkan Mobayen Khiabani

Reputation: 34170

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

Upvotes: 1

Related Questions