0

Can i get a litle help with my problem?

I want to write all of my textbox values in a txt file.

In a normal situation when the textboxes is outside of the tabcontrol or panel i use this code what i copied in, but this lines do not work when the textboxes is in a panel or tabcontrol page.

The goal is: I want all of the values to return when i restart my program, how can i do it ?

//To write textbox values to file (may be used on form close)
using (System.IO.StreamWriter sw = new StreamWriter("values.txt"))
{
    foreach (var control in this.Controls)
    {
        if (control is TextBox)
        {
            TextBox txt = (TextBox)control;
            sw.WriteLine(txt.Name + ":" + txt.Text);
        }
    }
}


//To read textbox values from file (may be used on form load)
using (System.IO.StreamReader sr = new StreamReader("values.txt"))
{
    string line = "";
    while((line=sr.ReadLine()) != null)
    {
        //Ugly, but work in every case
        string control = line.Substring(0, line.IndexOf(':') );
        string val =  line.Substring(line.IndexOf(':') + 1);

        if(this.Controls[control] != null)
        {
            ((TextBox)this.Controls[control]).Text = val;
        }
    }
}
  • https://stackoverflow.com/questions/15186828/loop-through-all-controls-of-a-form-even-those-in-groupboxes – dr.null Jul 25 '23 at 09:29

0 Answers0