简体   繁体   English

如果c#中包含一些按钮,如何保存动态创建的用户控件?

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

I am new to C#. 我是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). 我正在使用Windows窗体,并且我拥有包含两个按钮的Form1(一个在运行时创建用户控件,另一个在运行时创建用户控件的按钮)。

This code creates user control and FlowLayoutPanel (to organize button position) if you click add_UserControl button. 如果单击add_UserControl按钮,此代码将创建用户控件和FlowLayoutPanel (以组织按钮位置)。 And then it creates buttons on FlowLayoutPanel if you click Add_Buttons button and it is all done at run time. 然后,如果您单击Add_Buttons按钮,它将在FlowLayoutPanel上创建按钮,并且所有这些操作均在运行时完成。

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? 现在在Form1我创建了用户控件和FlowLayoutPanel ,然后创建了5个按钮,如何在SQL数据库中将其用户控件的属性/细节及其FlowLayoutPanel和5个按钮保存到SQL数据库中,以便以后可以使用它们? 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. 然后,您将需要一些其他代码来监视该集合,并且每当它收到一个新条目时,它将创建一个新按钮并将其Button文本设置为text属性。 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. 如果希望能够重新加载它,则可以将MyButton类定义为可序列化并将其存储在xml文件中,并在构建时重新加载它。

You should watch into WPF and it's MVVM pattern. 您应该注意WPF及其MVVM模式。 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. 您可以在一个SQL表中记住FlowLayoutsPanels,在另一个表中可以保存属于这些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. 在“表单加载”或“应用程序加载”上,可以检查SQL db中是否已经存在FlowLayoutPanels和相应的按钮,如果是,则创建它们,否则什么也不做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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