how to make a reading operation with this writing textfile solution, please?

Textbox[]tbs = { textBox1, textBox2, textBox3, textBox4, textBox5 }; // add all of them in here
StringBuilder sb = new StringBuilder();
foreach(TextBox tb in tbs)
     sb.AppendLine(tb.Text);
System.IO.File.WriteAllText(@"C:\myFile.txt", sb.ToString());

Do you mean a read of the file?
Try ReadAllText

Yes, but how with readalltext. I have many textboxes. Look the précedent message it's ok for saving into the text file but i don't how to read text file to send into textbox1, textbox2, textbox3 etc...

StringBuilder AppenLine method appends a cr after every string of a textbox.
So you can use the string Split method to separate al the different textbox strings.Look it up in MSDN to find out how the Split method works.

Thanks ddanbe but i don't need the string split method i want just import variables of the myFile.txt(look my writing code of myFile.txt) to texboxes.

This is what I meant:

TextBox textBox1 = new TextBox();
            TextBox textBox2 = new TextBox();
            TextBox textBox3 = new TextBox();
            TextBox textBox4 = new TextBox();
            TextBox[] tbs = { textBox1, textBox2, textBox3, textBox4}; 

            // suppose this string is read from a file
            string StrReadFromFile = "TB1text" + "\n" + "TB2text" + "\n" + "TB3txt" + "\n" + "TB4txt" +"\n";
            string[] tbxStrings = StrReadFromFile.Split(new char[]{'\n'});
            //Drop the splitted tekst into the textboxes
            for (int i = 0; i < tbs.Length; i++)
            {
                tbs[i].Text = tbxStrings[i];
            }

Hope some code will make it clearer.

Another way to do this would be:

TextBox textBox1 = new TextBox();
            TextBox textBox2 = new TextBox();
            TextBox textBox3 = new TextBox();
            TextBox textBox4 = new TextBox();
            TextBox[] tbs = { textBox1, textBox2, textBox3, textBox4}; 

            // suppose this string is read from a file
            string StrReadFromFile = "TB1text" + "\n" + "TB2text" + "\n" + "TB3txt" + "\n" + "TB4txt" +"\n";

            //Drop the splitted tekst into the textboxes
            tbs[0].Text = (StrReadFromFile.Split('\n'))[0];
            tbs[1].Text = (StrReadFromFile.Split('\n'))[1];
            tbs[2].Text = (StrReadFromFile.Split('\n'))[2];
            tbs[3].Text = (StrReadFromFile.Split('\n'))[3];  
if(openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
    {
    TextBox textBox1 = new TextBox();
    TextBox textBox2 = new TextBox();
    TextBox textBox3 = new TextBox();
    TextBox textBox4 = new TextBox();
    TextBox[] tbs = { textBox1, textBox2, textBox3, textBox4}; 
    // suppose this string is read from a file
    string StrReadFromFile = "TB1text" + "\n" + "TB2text" + "\n" + "TB3txt" + "\n" + "TB4txt" +"\n";
    string[] tbxStrings = StrReadFromFile.Split(new char[]{'\n'});
    //Drop the splitted tekst into the textboxes
    for (int i = 0; i < tbs.Length; i++)
    {
    tbs[i].Text = tbxStrings[i];
    }
    }

Sorry but it's not working for me. I think i forget something if you can help me please.

if(openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
            {
                TextBox TextBox1 = new TextBox();
                TextBox TextBox2 = new TextBox();
                TextBox TextBox3 = new TextBox();
                TextBox TextBox4 = new TextBox();
                TextBox[] tbs = { TextBox1, TextBox2, TextBox3, TextBox4};
                // suppose this string is read from a file
                string StrReadFromFile = "TB1text" + "\n" + "TB2text" + "\n" + "TB3txt" + "\n" + "TB4txt" +"\n";
                string[] tbxStrings = StrReadFromFile.Split(new char[]{'\n'});
                //Drop the splitted tekst into the textboxes
                for (int i = 0; i < tbs.Length; i++)
                {
                    tbs[i].Text = tbxStrings[i];
                }
                }

Could you please tell, WHAT is not working?

there's nothing in my textboxes.

The code I gave you was just a raw sketch of what it could look like.
It is up to you to fill in the details, size, font etc.
Here is (again minimal but working code)

TextBox TextBox1 = new TextBox();
        TextBox TextBox2 = new TextBox(); TextBox2.Location = new Point(50, 50);
        TextBox TextBox3 = new TextBox(); TextBox3.Location = new Point(50, 80);
        TextBox TextBox4 = new TextBox(); TextBox4.Location = new Point(100, 100);
        TextBox[] tbs = { TextBox1, TextBox2, TextBox3, TextBox4};
        // suppose this string is read from a file
        string StrReadFromFile = "TB1text" + "\n" + "TB2text" + "\n" + "TB3txt" + "\n" + "TB4txt" +"\n";
        string[] tbxStrings = StrReadFromFile.Split(new char[]{'\n'});
        //Drop the splitted tekst into the textboxes
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i].Text = tbxStrings[i];
        }
        //the form needs to know his controls
        this.Controls.AddRange(tbs); 

I have placed this code right after the initializeComponent call of the form constructor.

Ok, Thank's. I found another solution and it's better.

if(openFileDialog1.ShowDialog() ==System.Windows.Forms.DialogResult.OK)
            {
                StringBuilder loaded = new StringBuilder();
                StreamReader reader = new StreamReader(openFileDialog1 .FileName );
                String line;
                // Read and display lines from the file until the end of
                // the file is reached.
                while ((line = reader.ReadLine()) != null)
                {
                    String[] content = line.Split('#');
                    String name = content[0];
                    String value = content[1];
 
                    Control control = Controls[name];
                    if (control is TextBox)
                        (control as TextBox).Text = value;
                    else if (control is CheckBox)
                        (control as CheckBox).Checked = (value.Equals("true", StringComparison.OrdinalIgnoreCase));
                }                           
            }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.