Swap line break " n" for " r n" in Richtextbox

Asked

Viewed 3,064 times

5

I have a RichTextBox that needs to count the breaks of lines as "\r\n", but she’s counting as "\n".

Example:

"Hello, (User squeezes Enter)world!"

To RichTextBox interpret like this:

"Olá," + "\n" + "mundo!"

And I need:

"Olá," + "\r\n" + "mundo!"

I had done the following, so that at the time I save the text of RichTexBox, the breaks were as "\r\n": I send the text of RichTexBox to an array of strings called TextoTraduzido (In case you can go saving the texts in the array), and:

for (int i = 0; i < (TextoTraduzido.Length); ++i)
{
    string TextoTraduzidoSeparado = TextoTraduzido[i].Split(separadores, StringSplitOptions.RemoveEmptyEntries);
    TextoTraduzido[i] = string.Join(System.Environment.NewLine, TextoTraduzidoSeparado);
}

That solved the problem at first, but then I noticed that I also need the line break to look like "\r\n" while the user is typing.

The typed text will be part of a hexadecimal code, it cannot contain more or less characters. " r n" are two characters and " n" is only one. For example, if I type a text with 6 characters and a line break, man label with RichTextBox1.Text.Length will count as 7 instead of 8. Until I copy and paste the text from RichTextBox for another text editor, the text will be less characters, and need these values in the correct format.

There is no way to change the line break character of RichTextBox? Or else, what would be the best way to replace the "\n" for "\r\n" while the user types?

  • 1

    Why do you need to replace while the user type? What’s the point?

  • @Joãomartins, I edited the question. Take a look there please

  • Right, so just replace the character " n" when you fetch the value of RichTextBox, right?

2 answers

2


I decided to change the RichTextBox by a TextBox.

1

Make the change with one of the method overloads String.Replace

When you need to change the line break:

string TextoComQuebrasTrocadas = SeuRichTextBox.Text.Replace("\n","\r\n");

̶o̶u̶ ̶d̶i̶r̶e̶t̶o̶ ̶n̶o̶ ̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶:̶ ̶

̶ ̶ ̶ ̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶T̶e̶x̶t̶ ̶=̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶T̶e̶x̶t̶.̶R̶e̶p̶l̶a̶c̶e̶(̶"̶\̶n̶"̶,̶"̶\̶r̶\̶n̶"̶)̶;̶

̶ ̶̶̶C̶a̶s̶o̶ ̶q̶u̶e̶i̶r̶a̶ ̶u̶s̶a̶r̶ ̶d̶e̶n̶t̶r̶o̶ ̶d̶o̶s̶ ̶e̶v̶e̶n̶t̶o̶s̶ ̶d̶e̶ ̶T̶e̶x̶t̶C̶h̶a̶n̶g̶e̶d̶ ̶o̶u̶ ̶K̶e̶y̶P̶r̶e̶s̶s̶:̶̶̶ ̶

̶ ̶ ̶ ̶ ̶p̶r̶i̶v̶a̶t̶e̶ ̶v̶o̶i̶d̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶_̶K̶e̶y̶P̶r̶e̶s̶s̶(̶o̶b̶j̶e̶c̶t̶ ̶s̶e̶n̶d̶e̶r̶,̶ ̶K̶e̶y̶P̶r̶e̶s̶s̶E̶v̶e̶n̶t̶A̶r̶g̶s̶ ̶e̶)̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶{̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶/̶/̶ ̶S̶a̶l̶v̶a̶ ̶a̶ ̶p̶o̶s̶i̶ç̶ã̶o̶ ̶a̶t̶u̶a̶l̶ ̶d̶o̶ ̶c̶u̶r̶s̶o̶r̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶i̶n̶t̶ ̶i̶ ̶=̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶S̶e̶l̶e̶c̶t̶i̶o̶n̶S̶t̶a̶r̶t̶;̶
̶ ̶ ̶ ̶ ̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶T̶e̶x̶t̶ ̶=̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶T̶e̶x̶t̶.̶R̶e̶p̶l̶a̶c̶e̶(̶"̶\̶n̶"̶,̶ ̶"̶\̶r̶\̶n̶"̶)̶;̶
̶ ̶ ̶ ̶ ̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶/̶/̶A̶t̶u̶a̶l̶i̶z̶a̶ ̶a̶ ̶p̶o̶s̶i̶ç̶ã̶o̶ ̶d̶o̶ ̶c̶u̶r̶s̶o̶r̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶S̶e̶u̶R̶i̶c̶h̶T̶e̶x̶t̶B̶o̶x̶.̶S̶e̶l̶e̶c̶t̶i̶o̶n̶S̶t̶a̶r̶t̶ ̶=̶ ̶i̶;̶
̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶ ̶}̶
  • 1

    It didn’t work very well... I put in Textchanged and when typing something like "Testing 123", it appears "321 odnatseT" because of the constant update...

  • @ANDREYMOURADASILVA If you change the text inside the text event the cursor does not move. You have to compensate for the position manually.

  • @ANDREYMOURADASILVA I made a compensating edit of the cursor position.

  • Now the cursor moves, but the " n" is not being replaced by " r n".

  • I’ve been reading the forums about Visual Studio and discover that the RichTextBox does not allow line terminator other than \n. But you can at any time get a string with line terminator replacement.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.