1

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

Damith
  • 62,401
  • 13
  • 102
  • 153
Arsalan Saleem
  • 321
  • 2
  • 6
  • 21
  • 1
    You can't save the button directly, but you can save the details that went into making the button, namely `x`, `y` and `name`. – Adam Houldsworth May 24 '12 at 14:37
  • 1
    so you mean I can not reload the buttons once created by the user?? – Arsalan Saleem May 24 '12 at 14:38
  • Continuing @AdamHouldsworth : And then load all saved details from that some storage, and re-create the buttons according to those loaded details on the beginning of the running. – SimpleVar May 24 '12 at 14:39
  • You can reload them, but you cannot save the button as an entity and expect it to re-associated parents and handles. Better to save the details that went into creating the button, and load them from file when you need to by calling your method again. – Adam Houldsworth May 24 '12 at 14:39
  • @AdamHouldsworth ok ok so this what I concluded,, I must save details and the number of buttons created and then through iteration i could recreate them,, if so a question: where else i could save the details except db and xml any other choices ?? – Arsalan Saleem May 24 '12 at 14:43
  • @jrb i tried but i could not use xamlWriter in windows forms,, i may not be correct but this is to some extent is only possible in wpf as we have to put button into string which is only possible through xaml writer please correct me if i am wrong – Arsalan Saleem May 24 '12 at 14:46
  • `XamlWriter` is not what he meant. You could use `XmlDocument` to build the XML document - or you read my answer properly and try using a `StringCollection` *user setting*. – Thorsten Dittmar May 24 '12 at 14:55
  • yeah user setting seem to be a possible solution for my problem.. i am trying it i will post back if i would get any problem thanks.. – Arsalan Saleem May 24 '12 at 14:57
  • I've edited my answer and added lots of code and explanations to get you started. – Thorsten Dittmar May 24 '12 at 15:02
  • Try storing the data about the button to a text file and retrieving the data when the application is re-started. That's the simplest way. Not very professional though =] If you want to know how just ask me, I can e-mail you as I find that much easier, the characters on this limit me for the answer. Hope this helps. – user959631 May 24 '12 at 15:02
  • @user959631: You know that you could actually write an answer, right? – Thorsten Dittmar May 24 '12 at 15:12
  • @user959631 yes please where i can provide you email.. if you can answer here,, that would be great – Arsalan Saleem May 24 '12 at 15:27

3 Answers3

2

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]);
        }
    }
}
Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
0

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.

Damith
  • 62,401
  • 13
  • 102
  • 153
0

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 } };
    }
jrb
  • 1,708
  • 2
  • 13
  • 20
  • 1
    XmlNode nameNode = settings.SelectSingleNode("name"); XmlNode xNode = settings.SelectSingleNode("x"); XmlNode yNode = settings.SelectSingleNode("y"); where to find settings is these are the same setting Thorsten Dittmar was talking about?? – Arsalan Saleem May 24 '12 at 15:12
  • No, this is an xml solution that has nothing to do with what I've suggested. It is just as good, but it's different from doing it with user settings. – Thorsten Dittmar May 24 '12 at 16:08
  • Sry, 'setting' is supposed to be 'button' i have updated the example – jrb May 28 '12 at 12:27