【问题标题】:How I can save controls created in run time in Windows Forms如何在 Windows 窗体中保存运行时创建的控件
【发布时间】:2012-05-24 14:35:06
【问题描述】:

这是我的代码

private void make_Book(int x, int y, string name)
{
    #region Creating Book

    // this code is initializing the book(button)
    Button book1 = new Button();
    Image img = button1.Image;
    book1.Image = img;
    book1.Name = name;
    book1.Height = img.Height;
    book1.Width = img.Width;
    book1.Location = new Point(44 + x, 19 + y);            
    book1.Click += new EventHandler(myClickHandler);
    groupBox1.Controls.Add(book1);

    #endregion            
}

每次我点击一个按钮时,这段代码都会创建一个按钮,现在我想保存创建的按钮及其属性,以便每次应用程序启动时它们都会出现..

在 C# Visual Studio 2010 中编码...

【问题讨论】:

  • 你不能直接保存按钮,但是你可以保存制作按钮的细节,即xyname
  • 所以你的意思是我不能重新加载用户创建的按钮??
  • Continuing @AdamHouldsworth :然后从某个存储中加载所有保存的详细信息,并根据运行开始时加载的详细信息重新创建按钮。
  • 您可以重新加载它们,但您不能将按钮保存为实体并期望它重新关联父项和句柄。最好保存创建按钮的详细信息,并在需要时通过再次调用您的方法从文件中加载它们。
  • @AdamHouldsworth 好的,这就是我的结论,我必须保存详细信息和创建的按钮数量,然后通过迭代我可以重新创建它们,如果是这样的话:我还能在哪里保存除了 db 和 xml 的细节还有其他选择吗??

标签: c# winforms


【解决方案1】:

一种解决方案可能是使用StringCollection 用户设置编辑:在您的评论中,您说关闭应用程序时这不会被保留。那不是真的,因为这是使用用户设置的全部意义......)。

在每一行中,需要将控件的位置和名称保存为字符串,例如like

120;140;MyName

当用户添加新按钮时,在StringCollection 中创建一个项目,如下所示:

private void make_BookButtonAndStore(int x, int y, string name)
{
    make_Book(x,y,name);

    Properties.Settings.Default.ButtonStringCollection.Add(String.Format("{0};{1};{2}", book1.Location.X, book1.Location.Y, book1.Name));
    Properties.Settings.Default.Save();
}

private void make_Book(int x, int y, string name)
{
    // this code is initializing the book(button)
    Button book1 = new Button();
    Image img = button1.Image;
    book1.Image = img;
    book1.Name = name;
    book1.Height = img.Height;
    book1.Width = img.Width;
    book1.Location = new Point(44 + x, 19 + y);            
    book1.Click += new EventHandler(myClickHandler);
    groupBox1.Controls.Add(book1);
}

然后您需要通过读取每一行、提取位置和名称并再次调用make_book 来从StringCollection 中的每个项目创建按钮的代码(不是我的新@987654327 @ 方法,因为这会使按钮加倍)。

请注意,在添加第一个按钮之前,您可能需要使用 new 关键字创建 StringCollection

编辑
解释如何创建这样的设置:转到您的项目属性的“设置”选项卡。创建一个名为ButtonStringCollection 的新设置,选择类型System.Collections.Specialized.StringCollection 和范围User

在表单的构造函数中,添加以下行:

if (Properties.Settings.Default.ButtonStringCollection == null)
    Properties.Settings.Default.ButtonStringCollection = new StringCollection();

然后,添加我在上面提供的代码来创建按钮。此外,在表单的 Load 事件处理程序中,添加如下内容:

foreach (string line in Properties.Settings.Default.ButtonStringCollection)
{
    if (!String.IsNullOrWhitespace(line))
    {
        // The line will be in format x;y;name
        string[] parts = line.Split(';');
        if (parts.Length >= 3)
        {
            int x = Convert.ToInt32(parts[0]);
            int y = Convert.ToInt32(parts[1]);

            make_Book(x, y, parts[2]);
        }
    }
}

【讨论】:

  • 谢谢哥们,但我认为只要我关闭应用程序,数据就会从字符串集合中删除
  • 不,不会。是说用户设置。您需要正确阅读答案。
  • 好的,谢谢,对不起,我在搜索之前没有听说过用户设置,顺便说一句谢谢..
  • 没问题。我已经编辑了答案并添加了很多可以帮助你的代码。
  • 我能从你的回答中问你一些问题吗?我将非常感谢! :)
【解决方案2】:

当您调用make_Book 方法时,您可以将输入参数保存到数据库或您的应用程序当前使用的一些其他存储中。当应用程序启动时,您可以通过调用 make_Book 方法加载所有按钮,并将值保存在应用程序存储中。

【讨论】:

  • 嗯,让我试试这个,我会在这里再问一次,这个答案真的进入了我的脑海;)
  • 这正是其他人想要告诉你的!
【解决方案3】:

这是一个如何保存加载 xml 的示例。

public static void Save(string x, string y, string name)
    {
        if (!Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\appName"))
        {
            Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\appName");
        }


        XmlDocument xmlDocument = new XmlDocument();

        string xml = string.Format(@"<?xml version='1.0' encoding='utf-8'?><button><x>{0}</x><y>{1}</y><name>{2}</name></button>", x, y, name);

        xmlDocument.LoadXml(xml);

        xmlDocument.Save(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\appName\\button.xml");
    }

    public static Dictionary<string,string> Load()
    {
        string address = "";

        if (!File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\appName\\button.xml"))
        {
            return new Dictionary<string,string>(){{"x",""},{"y",""},{"name",""}};
        }

        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\appName\\button.xml");

        XmlNode button = xmlDocument.GetElementsByTagName("button").Item(0);

        XmlNode nameNode = button.SelectSingleNode("name");
        XmlNode xNode = button.SelectSingleNode("x");
        XmlNode yNode = button.SelectSingleNode("y");

        return new Dictionary<string, string>() { { "name", nameNode.InnerText }, { "x", xNode.InnerText }, { "y", yNode.InnerText } };
    }

【讨论】:

  • XmlNode nameNode = settings.SelectSingleNode("name"); XmlNode xNode = settings.SelectSingleNode("x"); XmlNode yNode = settings.SelectSingleNode("y");在哪里可以找到设置,这些设置与 Thorsten Dittmar 所说的设置相同吗??
  • 不,这是一个 xml 解决方案,与我的建议无关。它一样好,但它与使用用户设置不同。
  • 对不起,“设置”应该是“按钮”,我已经更新了示例
猜你喜欢
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 1970-01-01
  • 2012-04-22
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多