Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

c# - How I can save controls created in run time in Windows Forms

here's my code

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..

coded in C# visual studio 2010...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One solution could be to use a StringCollection user setting (EDIT: 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

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

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).

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...