Burgo855
Burgo855

Reputation: 248

Save multiple richtextboxes using

I have been trying something new, basically I stumbled across this: http://www.homeandlearn.co.uk/csharp/csharp_s4p11.html

My form has a save as option as well as multiple tabs. I would like to save all of the richtextboxes to the text file. But when I add more than one richtextbox it just only saves the bottom one so I guess the formula is wrong?

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    string Saved_File = "";

    saveFD.InitialDirectory = "C:";
    saveFD.Title = "Save your file as...";
    saveFD.FileName = "";
    saveFD.Filter = "Text (*.txt)|*.txt|All Files(*.*)|*.*";
    if (saveFD.ShowDialog() != DialogResult.Cancel)
    {
        Saved_File = saveFD.FileName;
        richTextBox1.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
        richTextBox2.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
        richTextBox3.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
        richTextBox53.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
    }
}

In the above example it would only save richTextBox53, why doesn't it save all of the above richTextBoxes? and how does one go about saving all of them to a text file?

Upvotes: 1

Views: 718

Answers (3)

Edper
Edper

Reputation: 9322

Try this one

        string Saved_File = "";

        saveFD.InitialDirectory = "C:";
        saveFD.Title = "Save your file as...";
        saveFD.FileName = "";
        saveFD.Filter = "Text (*.txt)|*.txt|All Files(*.*)|*.*";
        if (saveFD.ShowDialog() != DialogResult.Cancel)
        {
            Saved_File = saveFD.FileName;
            RichTextBox allrtb = new RichTextBox();
            // This one add new lines using the "\n" every time you add a rich text box
            allrtb.AppendText(richTextBox1.Text + "\n" + richTextBox2.Text + "\n" + richTextBox3.Text+ "\n" + richTextBox53.Text );
            allrtb.SaveFile(Saved_File, RichTextBoxStreamType.PlainText);
        }

Now if you want spaces between the richtextbox just change the new line (i.e. \n) to space like one below:

allrtb.AppendText(richTextBox1.Text + " " + richTextBox2.Text + " " + richTextBox3.Text+ " " + richTextBox53.Text );

Upvotes: 0

Alaa Jabre
Alaa Jabre

Reputation: 1883

As I said in the comment, overriding is default behavior for the stream in saveFile function
so you need to specify the file mode.

System.IO.FileStream fs = 
                    new System.IO.FileStream(Saved_File,
                        System.IO.FileMode.OpenOrCreate,System.IO.FileAccess.ReadWrite);
    richTextBox1.SaveFile(fs, Saved_File);
        richTextBox2.SaveFile(fs, Saved_File);
        richTextBox3.SaveFile(fs, Saved_File);
        richTextBox53.SaveFile(fs, Saved_File);
fs.Close();

Upvotes: 1

Damith
Damith

Reputation: 63065

using (StreamWriter writer = File.AppendText(saveFD.FileName))
{
    richTextBox1.SaveFile(writer , RichTextBoxStreamType.PlainText);
    richTextBox2.SaveFile(writer , RichTextBoxStreamType.PlainText);
    richTextBox3.SaveFile(writer , RichTextBoxStreamType.PlainText);
    richTextBox53.SaveFile(writer , RichTextBoxStreamType.PlainText);
}

Upvotes: 1

Related Questions