如何在Windows窗体中保存在运行时创建的控件 [英] How I can save controls created in run time in Windows Forms

查看:65
本文介绍了如何在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            
}

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

this code is making a button every time I click on a button,, now i want to save the created button and its property so that they could appear every time application starts..

在C#visual studio 2010中编码...

coded in C# visual studio 2010...

推荐答案

一种解决方案可能是使用 StringCollection 用户设置(在您的注释中您是说关闭应用程序时将不会保留该消息.这是不正确的,因为这是使用用户设置的全部要点... ).

One solution could be to use a StringCollection user setting ( In your comment you're saying that this will not be persisted when closing the application. That's not true, as this is the entire point of using user settings...).

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

In every line, you need to save the position and the name of a control as a string, for example like

120;140;MyName

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

When the user adds a new button, create an item in the StringCollection like so:

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);
}

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

Then you'd need code that creates the buttons from every item in the StringCollection by reading each line, extracting the location and name and calling make_book again (not my new make_BookButtonAndStore method, as this would double the button).

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

Note that you may need to create the StringCollection with the new keyword before adding the first button.

编辑
要解释如何创建这样的设置,请执行以下操作:转到项目属性的设置"选项卡.创建一个名为 ButtonStringCollection 的新设置,选择类型 System.Collections.Specialized.StringCollection 和范围 User .

EDIT
To explain how to create such a setting: Go to your project properties to the "Settings" tab. Create a new setting named ButtonStringCollection, select type System.Collections.Specialized.StringCollection and scope User.

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

In your form's constructor, add the following line:

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

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

Then, add the code I've provided above to create the buttons. Also, in the form's Load event handler, add something like the following:

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]);
        }
    }
}

这篇关于如何在Windows窗体中保存在运行时创建的控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆