0

I am making a C# application in which I copy text from a Rich TextBox using

if(!string.IsNullOrEmpty(richTextBox1.Text))
        {
            Clipboard.SetText(richTextBox1.Text);

        }

I set the Rich TextBox code as follows

richTextBox1.Text = file + Environment.NewLine + r + Environment.NewLine + r1 + Environment.NewLine + r2 + Environment.NewLine + r3 + Environment.NewLine + r4 + Environment.NewLine + r5 + Environment.NewLine + r6 + Environment.NewLine + r7 + Environment.NewLine + r8 + Environment.NewLine + r9 + Environment.NewLine + r10 + Environment.NewLine + r11 + Environment.NewLine + r12 + Environment.NewLine + r13;

Also, I save the Rich TextBox text to a .txt file using

if (!string.IsNullOrEmpty(richTextBox1.Text))
         { SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Text File|*.txt";
            saveFileDialog1.Title = "Export EXIF Data";

            //  saveFileDialog1.RestoreDirectory = true;
           DialogResult save =  saveFileDialog1.ShowDialog();
            if (save == DialogResult.OK)
            {
                if (saveFileDialog1.FileName != "")
                {

                    File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);
                }

            }

Now the problem I am facing is that in the Rich TextBox, the text appears line by line where ever I have used Environment.NewLine for e.g

line 1
line 2
line 3

but after copying/saving, the .txt file appears like

line 1line 2line 3

Also, if I copy the text using KeyBoard shortuct Ctrl+C , the text appears fine in the text file.

I hope you understood my problem and would be able to help me. I can provide any more details if needed.

Update: I was able to fix the problem while saving the richTextBox text to a .txt file by using string[] instead of string like:

New Code:

 data = new String[] { file, r, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13 };
                 richTextBox1.Text = data[0] + Environment.NewLine + data[1] + Environment.NewLine + data[2] + Environment.NewLine + data[3] + Environment.NewLine + data[4] + Environment.NewLine + data[5] + Environment.NewLine + data[6] + Environment.NewLine + data[7] + Environment.NewLine + data[8] + Environment.NewLine + data[9] + Environment.NewLine + data[10] + Environment.NewLine + data[11] + Environment.NewLine + data[12] + Environment.NewLine + data[13] + Environment.NewLine + data[14];
//Saving:
File.WriteAllLines(saveFileDialog1.FileName, data);

Old Code:

richTextBox1.Text = file + Environment.NewLine + r + Environment.NewLine + r1 + Environment.NewLine + r2 + Environment.NewLine + r3 + Environment.NewLine + r4 + Environment.NewLine + r5 + Environment.NewLine + r6 + Environment.NewLine + r7 + Environment.NewLine + r8 + Environment.NewLine + r9 + Environment.NewLine + r10 + Environment.NewLine + r11 + Environment.NewLine + r12 + Environment.NewLine + r13;
//Saving:
File.WriteAllText(saveFileDialog1.FileName, richTextBox1.Text);

But still the copying is not working correctly

Haider Ali Punjabi
  • 345
  • 1
  • 3
  • 13
  • 6
    What do you mean by "the .txt file appears like" - how are you checking it? Have you looked at it in a binary file editor to see what the actual bytes are? – Jon Skeet Aug 12 '15 at 12:51
  • 1
    Does this have anything to do with the save dialog or WriteAllText? It seems that richTextBox1.Text is not what you expect. Why are you using a rich text box for unformatted text? – usr Aug 12 '15 at 12:54
  • possible duplicate of [Difference between "\n" and Environment.NewLine](http://stackoverflow.com/questions/1015766/difference-between-n-and-environment-newline) – Owen Pauling Aug 12 '15 at 12:55
  • I am able to copy and save the text from the richTextbox to a .txt file and open it with notepad. The problem is when saving/copying it via code, the text is not placed line by line, but in the same single line. – Haider Ali Punjabi Aug 12 '15 at 12:56
  • When you're in the debugger, does 'richTextBox1.Text' save and preserve the newlines? – dpimente Aug 12 '15 at 13:09

1 Answers1

1

Try this:

File.WriteAllLines(saveFileDialog1.FileName, richTextBox1.Lines);
Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
  • You may be onto it. According to msdn, under 'Remark's for the RichTextBox.Text property; "To display multiple lines of text in a RichTextBox, set the Multiline property to true. To read or set the text of a multiline text box, use the Lines property." https://msdn.microsoft.com/en-us/library/system.windows.forms.richtextbox.text(v=vs.110).aspx – dpimente Aug 12 '15 at 14:53
  • Oh!!! I think it would also work but I already solved the saving problem by using string arrays. Now I only need to fix the copying problem – Haider Ali Punjabi Aug 12 '15 at 15:22