Search code examples
c#winformssaveflowlayoutpanel

c# Save flow layout panel


I am programming in Visual Studio 2013, c# winform. Im trying to make something like Steam Library, but I don't know how to save FlowLayoutPanel that I have in tab1 (Library).

This is how it looks (Library)

This is how it looks (Adding a new game)

This is how it looks (Deleting a new game): http:// oi62.tinypic.com/2uzfc3k.jpg

(sorry, im not able to add images and more than 2 links)

Here is my code:

    private void btnTest_Click_1(object sender, EventArgs e)
    {
        if (textBox1.Text != "")
             {
            if (textBox2.Text != "")
                {
                    if (textBox3.Text != "")
                        {
                            Button btn = sender as Button;
                            Button btnNew = new Button();
                            btnNew.Text = "";
                            btnNew.Height = 108;
                            btnNew.Width = 230;

                            btnNew.Name = textBox3.Text;
                            comboBox1.Items.Add(textBox3.Text);

                            btnNew.BackgroundImage = new Bitmap(textBox1.Text);
                            btnNew.BackgroundImageLayout = ImageLayout.Stretch;
                            btnNew.FlatStyle = FlatStyle.Flat;
                            flpContainer.Controls.Add(btnNew);
                            btnNew.Click += btnNew_Click;
                            btnNew.Tag = textBox2.Text;
                            counter1+=+1;
                            label1.Text = counter1.ToString();
                            System.Windows.Forms.MessageBox.Show("Game " + textBox3.Text + " was successfully added to library!");
                            textBox1.Text = "";
                            textBox2.Text = "";
                            textBox3.Text = "";
                        }
                    else if (textBox3.Text == "")
                        {
                            MessageBox.Show("You didn't wrote name!");
                        }
                    }
            else if (textBox2.Text == "")
                {
                System.Windows.Forms.MessageBox.Show("You didn't choose exe file!");
                }
             }
        else if (textBox1.Text == "")
            {
                System.Windows.Forms.MessageBox.Show("You didn't choose image!");
            }
    }

    private void btnNew_Click(object sender, EventArgs e)
        {
            Button clickedButton = (Button)sender;
            Process.Start((string)clickedButton.Tag); 
        }

    private void ZvolitObrazek_Click(object sender, EventArgs e)
    {
        openFileDialog1.Title = "Open Image";
        openFileDialog1.FileName = "Image file";
        openFileDialog1.Filter = "Image files (*.jpg, *.img, *.png, *.jpeg)|*.jpg; *.img; *.png; *.jpeg|All files (*.*)|*.*";
       if (openFileDialog1.ShowDialog() == DialogResult.OK)
           {
           textBox1.Text = openFileDialog1.FileName;
           }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        openFileDialog2.Title = "Open exe";
        openFileDialog2.FileName = "Exe file";
        openFileDialog2.Filter = "Exe files (*.exe)|*.exe|All files(*.*)|*.*";

        if (openFileDialog2.ShowDialog() == DialogResult.OK)
        {
            textBox2.Text = openFileDialog2.FileName;
        }
    }

    private void flpContainer_Paint(object sender, PaintEventArgs e)
    {
        flpContainer.AutoScroll = true;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        if (comboBox1.Text == "")
        {
            MessageBox.Show("You didn't choose game that you want delete!");
        }

        else if (comboBox1.Text  != "")
        {
            Control foundControl = null;
            foreach (Control c in flpContainer.Controls)
            {
                c.Name = comboBox1.Text;
                foundControl = c;
            }
            flpContainer.Controls.Remove(foundControl);
            counter1 = counter1 - 1;
            label1.Text = counter1.ToString();
            MessageBox.Show("Game " + comboBox1.Text + " was successfully deleted");
            comboBox1.Items.Remove(comboBox1.Text);
            comboBox1.Text = "";
        }
    }
}

FlowLayoutPanel=flpContainter. So, my question is, how can i save items (buttons) in FlowLayoutPanel and how to load them later? Thank you for your answers!


Solution

  • You should create a class for your items (game buttons), including their Title, Image etc. Then you can save them using XML.

    class Game { // Properties here }

    This link will provide you with a quick How-to on how to accomplish this. For saving images you can convert the image to base64 and convert it back to an image when loading the XML file again.