Home > Software design >  How to keep the "\n" format
How to keep the "\n" format

Time:12-04

Creating text in notepad

`

 private void sSubmit_Click(object sender, EventArgs e)
        {
            TextWriter txt = new StreamWriter(@"C:\Users\Dat.txt", true);
            txt.Write(sTxtSurname.Text   ", "   sTxtFirstname.Text   "\n\n");
            txt.Close();
        }

`

displaying the text in a textbox `

public void ReadFile()
        {

            TextReader reder = File.OpenText(@"C:\Users\Dat.txt");
            textBox1.Text = reder.ReadToEnd();
        }

`

it wont diplay the "\n"

for example, i put my name and age

when displaying, it should seperate the name and age, but it doesnt

`

Outputs:
   Jiin Taq 19

`

`

Desired Output:
   Jiin Taq
   19

`

CodePudding user response:

To keep the "\n" format when displaying text in a textbox, you can use the Environment.NewLine property instead of "\n" in your code. This property will insert a new line character that is appropriate for the current operating system.

For example, you could modify the ReadFile method in the following way:

public void ReadFile()
{
    TextReader reder = File.OpenText(@"C:\Users\Dat.txt");
    textBox1.Text = reder.ReadToEnd().Replace("\n", Environment.NewLine);
}

This will replace any "\n" characters in the text with the appropriate new line character for the current operating system, allowing the text to be displayed properly in the textbox.

  • Related