saving functions for a dynamically created button

61 Views Asked by At

I want to save the text and the individual button function created by the user during the execution of the program. To do this, I use Properties.Settings.Default and my created ButtonStringCollection collection.

I tried to use another form to fill it out, but I came to the InputBox. After closing the program, it "forgets" all the Text of the created buttons and they need to be re-entered, and nothing prevents changing the value. I want the values to be preserved but I don't know what mechanics can be used. All code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization.Formatters.Soap;
using System.IO;
using System.Collections.Specialized;
using Microsoft.VisualBasic;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        int x1 = 10;
        public string name;
        public Form1()
        {
            InitializeComponent();

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

        private void make_Book(int x, int y)
        {
            name = Interaction.InputBox("Enter Name", "", "", 300,300);
            Form2 form2 = new Form2();
            Button book1 = new Button();
           // book1.Name = name;
            book1.Text = name;
            book1.Height = 50;
            book1.Width = 70;
            book1.Location = new Point(44 + x, 19 + y);
            //book1.Click += new EventHandler(myClickHandler);
            flowLayoutPanel1.Controls.Add(book1);
            
        }
        private void make_BookButtonAndStore(int x, int y)
        {
            make_Book(x, y);

            Properties.Settings.Default.ButtonStringCollection.Add(String.Format("{0};{1};{2}", x, y, name));
            Properties.Settings.Default.Save();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            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);
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
          //  Form2 form2 = new Form2();
          //  form2.Show();
           // form2.ShowDialog();
            make_BookButtonAndStore(x1 += 80, 20);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flowLayoutPanel1.Controls.Count > 0)
            {
                flowLayoutPanel1.Controls.RemoveAt(0);
                Properties.Settings.Default.ButtonStringCollection.RemoveAt(0);
                Properties.Settings.Default.Save();
            }
            else
                MessageBox.Show("На панеле больше нет кнопок");
        }
    }
}
1

There are 1 best solutions below

4
Madax On

Problem Nr1 is:

name = Interaction.InputBox("Enter Name", "", "", 300, 300);

even if you want to abort, it reads the value from the textbox, creates a new button and adds it. So when the program starts, it reads your String collection, it HAS the saved data, then this method gets called, and it overwrites it. Thats the behavior I can see here. In WinForms: Create a small Form "AddButton" and call it like so:

AddButton addB = new AddButton()
if (addB.ShowDialog() == DialogResult.OK)
{
//do something...
}

While on the import function, immediatly add the button. I can't really see much more here, without the rest of the project.

Why is Form2 instantiated inside the scope of make_Book ? Where is flowLayoutPanel1 in your Solution?

It just seems the data is lost. To verify: Delete all settings, start the app, create a button("aaaaa"). Everything is ok. Restart the app, it reads the config, the original button "aaaaa" is there, now make_Book is called to add it, and now one of two things happens:

  1. do not enter anything into "Interaction.InputBox()" -> the text now is empty, and a blank button is createn in the "aaaaaa" location.
  2. enter something in "Interaction.InputBox()" BUT click ABORT -> it goes ahead and overwrites the "aaaaaa" button with whatever was in the textfield, and adds this instead.

Here is your fixed code. It is quite chaotic now, but I think u'll get it :) (I needed to change the name of your settings class) Didn't find anything else right now, without the rest of the code:

using System.Collections.Specialized;
using Microsoft.VisualBasic;

namespace WinFormsApp2
{
    public partial class Form1 : Form
    {
        Form2? form2 = null;

        public bool isImporting = false;
        int x1 = 10;
        public string name;

        public Form1()
        {
            InitializeComponent();

            if (Properties.Settings.Default.bsColl == null)
            {
                Properties.Settings.Default.bsColl = new StringCollection();
            }
        }
        private void make_Book(int x, int y, string name)
        {
            if(form2 is null)
            {
                form2 = new Form2();
            }

            Button book1 = new();

            if (!isImporting)
            {
                string boxValue = Interaction.InputBox("Enter Name", "", "", 300, 300);

                if (!string.IsNullOrEmpty(boxValue))
                {
                    book1.Name = boxValue;
                    book1.Text = boxValue;
                }
                else
                {
                    book1.Name = name;
                    book1.Text = name;
                }
                book1.Location = new Point(44 + x, 19 + y);
            }
            else
            {
                book1.Name = name;
                book1.Text = name;
                book1.Location = new Point(x, y);
            }

            book1.Height = 50;
            book1.Width = 70;

            //book1.Click += new EventHandler(myClickHandler);
            flowLayoutPanel1.Controls.Add(book1);

            if (!isImporting)
            {
                Properties.Settings.Default.bsColl.Add(String.Format("{0};{1};{2}", book1.Location.X, book1.Location.Y, book1.Name));
                Properties.Settings.Default.Save();
            }

        }
        private void make_BookButtonAndStore(int x, int y)
        {
        }// WE DONT NEED THIS

        private void Form1_Load(object sender, EventArgs e)
        {
            isImporting = true;

            foreach (string line in Properties.Settings.Default.bsColl)
            {
                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]);
                    }
                }
            }
            isImporting = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //  Form2 form2 = new Form2();
            //  form2.Show();
            // form2.ShowDialog();
            make_Book(x1 += 80, 20, "");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (flowLayoutPanel1.Controls.Count > 0)
            {
                flowLayoutPanel1.Controls.RemoveAt(0);
                Properties.Settings.Default.bsColl.RemoveAt(0);
                Properties.Settings.Default.Save();
            }
            else
                MessageBox.Show("На панеле больше нет кнопок");
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.bsColl = null;
            Properties.Settings.Default.Save();
        }
    }
}