Tim Schaeppi
Tim Schaeppi

Reputation: 51

Saving multiple text box entries to a single file, C#

How would I save text in richTextBox1 and richTextBox2 into the same file, which could be accessed to use those text entries as strings to make labels? Is it possible to also save the state of a checkbox along with the text from the richTextBoxes?

Upvotes: 1

Views: 1516

Answers (3)

Andreas Rohde
Andreas Rohde

Reputation: 609

Try this, this will save the values in a texfile

  var strFilePathAndName = "anyPathAndFilename"
  using (var fs = new System.IO.FileStream(strFilePathAndName, System.IO.FileMode.OpenOrCreate))
  {
     fs.Close();
  }

  // Create the writer for data.
  using ( var w = new System.IO.StreamWriter(strFilePathAndName, true))
  {
     w.Write(richTextBox1.Text);
     w.Write(richTextBox2.Text);
     w.Write(checkBox1.Checked.toString());
     w.Close();
  }

Upvotes: 1

Lost_In_Library
Lost_In_Library

Reputation: 3483

"Is it possible to also save the state of a checkbox along with the text from the richTextBoxes?" I don't get it, but is that what you want ?


richtextBox1.text = IsItPossibleToSaveCheckboxState.Checked.ToString();
richTextBox1.text will show the check state like True -or- False

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499790

You can save whatever you like in the file - you just need to work out what format you want to use. For example, you could use LINQ to XML:

var data = new XDocument("uistate",
    new XElement("tb1", richTextBox1.Rtf),
    new XElement("tb2", richTextBox2.Rtf),
    new XElement("cb", checkBox.Checked));
data.Save("uistate.xml");

Upvotes: 4

Related Questions