How I can save controls created in run time in Windows Forms

后端 未结 3 703
一整个雨季
一整个雨季 2020-12-12 03:30

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 b         


        
相关标签:
3条回答
  • 2020-12-12 03:36

    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]);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-12 03:44

    This is a sample of how to save load 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 } };
        }
    
    0 讨论(0)
  • 2020-12-12 03:49

    when you call make_Book method you can save input parameters to database or some other storage currently your application using. When application start you can load all the buttons by calling make_Book method with values saved on your application storage.

    • Save and Restore Setings of a .NET Form using XML
    • Load and save objects to XML using serialization
    • Windows Forms User Settings in C#
    0 讨论(0)
提交回复
热议问题