简体   繁体   中英

How can I save a dynamically created user control if it contains some buttons in c#?

I am new to C#. I am using windows forms and I have Form1 which contains 2 buttons ( one to create user control at run time and the other creates buttons on user control at run time).

This code creates user control and FlowLayoutPanel (to organize button position) if you click add_UserControl button. And then it creates buttons on FlowLayoutPanel if you click Add_Buttons button and it is all done at run time.

Now in Form1 let's say I created user control and FlowLayoutPanel and then created 5 buttons , how can I save the properties/details of this user control with its FlowLayoutPanel and 5 buttons in SQL database so I can use them later when I run the program? I have been thinking about an idea and I reached the internet but no luck.

Any idea? Please help me. Thank you

 public partial class Form1 : Form
{
    FlowLayoutPanel FLP = new FlowLayoutPanel();
    UserControl uc = new UserControl();


 private void add_UserControl_Click(object sender, EventArgs e)
    {

        uc.Height = 700;
        uc.Width = 900;
        uc.BackColor = Color.Black;
        Controls.Add(uc); //add UserControl on Form1


        FLP.Height = 600;
        FLP.Width = 800;
        FLP.BackColor = Color.DimGray;
        uc.Controls.Add(FLP); // add FlowLayoutPanel to UserControl
    }


    private void Add_Buttons_Click(object sender, EventArgs e)
    {

        //####### add buttons to FlowLayoutPanel ############

        Button dynamicButton = new Button();
        dynamicButton.Height = 50; 
        dynamicButton.Width = 200;
        dynamicButton.BackColor = Color.Green;
        dynamicButton.ForeColor = Color.Blue;
        dynamicButton.Text = "";
        FLP.Controls.Add(dynamicButton);



    }




}

OK, First you need to create a class that will represent one of the buttons with the properties you need.

class MyButton
{
    public string ButtonText {get;set;}
}

Everytime you click and create a button, you actually create an object of this class and add it to a collection or list. Then you would have some other code watching over the collection, and every time it gets a new entry, it creates a new button and sets its Button text to the text property. when a member of list is gone, it removes the button.

If you need more properties to be remembered (color, size, font, ...) you add them to the class as well. If you need for other controls, as well, .... you can always create common parent controls.

Simple.

If you want to be able to reload it, you could define the MyButton class as serializable and store it in xml file, and upon build, reload it.

You should watch into WPF and it's MVVM pattern. It's pretty much similar to it. Also have a look into command pattern, usefull pattern when it commes to this.

You can remember the FlowLayoutsPanels in one SQL table and in another table you could save the buttons which belong to these FlowLayoutPanels.

On Form Load or Application Load, you could check if there are already FlowLayoutPanels and correspending Buttons do exist in the SQL db and if yes then create them, else do nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM