如何在 Windows 窗体中保存运行时创建的控件



这是我的代码

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 中编码...

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

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

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不是我的新 make_BookButtonAndStore 方法,因为这会使按钮加倍)从StringCollection中的每个项目创建按钮的代码。

请注意,在添加第一个按钮之前,您可能需要使用 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]);
        }
    }
}

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

  • 使用 XML 保存和还原 .NET 表单的选址
  • 使用序列化将对象加载并保存到 XML
  • C# 中的 Windows 窗体用户设置

这是如何节省负载 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 } };
    }

最新更新