2

What would be the best way to save a windows form to a file with a few text boxes collecting user input. I using this at the moment:

if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                File.WriteAllText(saveFileDialog1.FileName, tB1.Text);
                File.WriteAllText(saveFileDialog1.FileName, tB2.Text);
            }

This is fine for saving the input from the first text box but when it comes to the other it wont save the data entered.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Tom
  • 19
  • 2
  • 8

6 Answers6

1

how about concatenating the two textboxes? for clarity,

string forSaving = tB1.Text + "\n" + tB2.Text;
File.WriteAllText(saveFileDialog1.FileName, forSaving);

or

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + tB2.Text);

UPDATE 1

string firstName = "FirstName: " + txtFirstName.Text;
string lastName = "LastName: " + txtLastName.Text;
string personAddress = "FirstName: " + txtAddress.Text;
string details = firstName + "\n" + lastName + "\n" + personAddress;
File.WriteAllText(saveFileDialog1.FileName, tB1.Text + "\n" + details);
John Woo
  • 258,903
  • 69
  • 498
  • 492
1

Concatenate this two texbox then;

File.WriteAllText(saveFileDialog1.FileName, tB1.Text + Environment.NewLine + tB2.Text );
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
1

The best way would probably be to create a method in your form that will return a string with all of the values from the TextBoxes into whatever format you want. Something like this would work:

File.WriteAllText(saveFileDialog1.fileName, OutputUserInfo());

Then inside OutputUserInfo() you can have whatever formatting to the data that you want so you can understand what they put in.

Edit Example of OutputUserInfo()

private string OutputUserInfo() {
    return  "First Name: " + tbFirstName.Text + Environment.NewLine +
            "Surname: " + tbSurname.Text + Environment.NewLine +
            "Address" + tbAddress.Text + Environment.NewLine;
            // Just keep adding whatever you want on here.
            // Add the descriptions if you want, it will probably help
}

You could also have this in different formats (CSV, or whatever). But if you're just doing a plain text file, this could be easiest. It is up to you though.

krillgar
  • 12,596
  • 6
  • 50
  • 86
  • To clear things up, I want to save all the input from numerous textboxs into one file e.g people entering personal information into a form such as first name, surname etc – Tom Jan 12 '13 at 16:05
  • Yeah, that would definitely help to have the method to output it as a string. I'll add an example to to my answer for you so you have a basic idea of what to do. – krillgar Jan 12 '13 at 16:12
  • I also want it to save at the click of a button sorry – Tom Jan 12 '13 at 16:14
  • That's fine. Having this method will help that if you want to have it save in multiple locations inside your code. Any time you want to save it, just call that method as the text that you are going to output to the file. – krillgar Jan 12 '13 at 16:19
1

File.WriteAllText is probably bad because it overwrites your content.

Creates a new file, writes the specified string to the file, and then closes the file. If the target file already exists, it is overwritten.

Instead go with File.AppendAllText which

Appends the specified stringto the file, creating the file if it does not already exist.

Ravi Y
  • 4,296
  • 2
  • 27
  • 38
1

If it was me I would use the StreamWriter / StreamReader Classes since they have WriteLine and Readline methods respectively.

i.e. something like this

private void button1_Click(object sender, EventArgs e)
{
    if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
        {
            sw.WriteLine(tB1.Text);
            sw.WriteLine(tB2.Text);
            sw.WriteLine(tB3.Text);
            sw.WriteLine(tB4.Text);
            sw.Close();
        }

   }

}

private void button2_Click(object sender, EventArgs e)
{
    if(openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
        {
            tB1.Text = sr.ReadLine();
            tB2.Text = sr.ReadLine();
            tB3.Text = sr.ReadLine();
            tB4.Text = sr.ReadLine();
            sr.Close();
        }
    }

}
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
  • Thanks you this is what i used and it worked! Thanks to everyone for there help to, really appreciated – Tom Jan 12 '13 at 16:33
1

There we go, use Encoding to Append all the string.

    private void button1_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string line = string.Format("{0},{1}"
            , textBox1.Text
            , textBox2.Text);
            File.AppendAllText(saveFileDialog1.FileName, line, Encoding.GetEncoding(1252));
        }
    }
Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44