Guest User

Guest User

  • Tech Writer
  • 13
  • 5.3k

WPF richtextbox no line break in first line?

Jan 31 2018 12:17 PM
I experienced a weird bug and am unable to solve it.
To reproduce:
1. Create new wpf project
2. Add richtextbox, name it e.g. "rtb"
3. Append a text with line break at the end 3 times. For example
  1. rtb.AppendText("lol\n");  
  2. rtb.AppendText("lol\n");  
  3. rtb.AppendText("lol\n");  
4. Now start the application. Notice how everything works fine and there are three lines saying "lol".
5. Now clear the richtextbox before appending the lines:
  1. rtb.Document.Blocks.Clear();  
  2. rtb.AppendText("lol\n");  
  3. rtb.AppendText("lol\n");  
  4. rtb.AppendText("lol\n");  
6. Start the application again. Notice that the output is no longer
   lol
   lol
   lol
but this time
   lollol
 
   lol
so essentially the "\n" for the first "lol" was added after the next "lol", lol?
Do you know what's going on because I don't. I'd expect it to output
   lol
   lol
   lol
and I don't see why clearing the text content makes the richtextbox behave like this. I guess it's a bug but I don't understand why it's happening or how can I solve this.
 
It can't be because of "rtb.Document.Blocks.Clear()" as I've tried other methods for clearing the richtextbox as well, like
 
  1. rtb.SelectAll();  
  2. rtb.Selection.Text = "";  
and other approaches. However, none of them afterwards printed the text correctly. Now in this simple scenario, doing
  1. rtb.Document.Blocks.Clear();  
  2. rtb.AppendText("lol\nlol\nlol\n");  
(writing all 'lol's at once) actually does fix the issue. However I obviously don't just print out lol, but I have to call "AppendText" multiple times, resulting in incorrect output. Any ideas? Or where should I submit a bug report? Thanks. 
 
 


Answers (4)