Search code examples
c#winformstextboxpasswords

How to save the text from a dynamically created textbox


Howdy I have a form that holds labels and textboxes that will be containing passwords to folders created by the program.Everytime they click on the save new passwordbutton it creates a new textbox and label on the form. I want to save the passwords so that if they make a change to the folder it will ask if they wish to keep the changes and then they have to enter the password for that folder.My question is how to save that password to that folder.they can add or delete the password from the settings if they wish.Here is the code I am using to create the new textboxes.The text in the boxes and labels are only for testing.The form that is called list is where the textboxes will showand that is only showing here for testing purposes

public partial class Passworddata : Form
{
    public List<TextBox> TextBoxes = new List<TextBox>();
    public List<Label> labels = new List<Label>();
    public Passworddata()
    {

        InitializeComponent();
    }

    private void Button1_Click(object sender, EventArgs e)
    { 
        TextBox tb = new TextBox();
        Label labl = new Label();

        int i = TextBoxes.Count + 1;
        tb.Location = new Point(30,i *30);
        labl.Location = new Point(1,i*30);
        tb.Width = 30;
        tb.Name = "ID" + i;
        labl.Text = "hi";
        tb.Text = "hello";
        TextBoxes.Add(tb);
        labels.Add(labl);
        Forms.list.Panel1.Controls.Add(tb);
        Forms.list.Panel1.Controls.Add(labl);
        Forms.list.Show();

    }




}

Solution

  • If you want to specifically save the controls and data, I would suggest the following:

    For saving:

    1. When the user adds a value, build a string like so:

      string currentEntry = tb.Name + "|" + tb.Text.Replace("|","~") + "|" + labl.Text;
      
    2. Then simply use the File.AppendAllText method, like so:

      File.AppendAllText("SavedData.dat", currentEntry);
      

    For loading:

    1. Subscribe to the Form.Load event for the Forms.list Form
    2. Read the saved data file if it exists
    3. Parse each line for the values needed
    4. Create and add the controls in the Form.Load

    So, to be clear, inside the Method that handles the Form.Load event for Forms.list you would have something like the following code:

        if(File.Exists("SavedData.dat"))
        {
            using (StreamReader sr = new StreamReader("SavedData.dat"))
            {
                string line = "";
                while((line = sr.ReadLine()) != null)  
                {
                    string[] lineData = line.Split('|');
                    if(lineData.Length == 3)
                    {
                        string currentTbName = lineData[0];
                        string currentTbText = lineData[1];
                        string currentLablText = lineData[2];
    
                        //LOAD THEM AS NORMAL
                    }
                }
            }
        }