title: How to append \line into RTF using RichTextBox control
body: <p>When using the Microsoft RichTextBox control it is possible to add new lines like this...</p>
  
  <pre><code>richtextbox.AppendText(System.Environment.NewLine); // appends \r\n
  </code></pre>
  
  <p>However, if you now view the generated rtf the \r\n characters are converted to \par not \line</p>
  
  <p>How do I insert a \line control code into the generated RTF?</p>
  
  <p><b>What does't work:</B></p>
  
  <p><b>Token Replacement</b></p>
  
  <p>Hacks like inserting a token at the end of the string and then replacing it after the fact, so something like this:</p>
  
  <pre><code>string text = "my text";
  text = text.Replace("||" "|"); // replace any '|' chars with a double '||' so they aren't confused in the output.
  text = text.Replace("\r\n", "_|0|_"); // replace \r\n with a placeholder of |0|
  
  richtextbox.AppendText(text);
  
  string rtf = richtextbox.Rtf;
  rtf.Replace("_|0|_", "\\line"); // replace placeholder with \line
  rtf.Replace("||", "|"); // set back any || chars to |
  </code></pre>
  
  <p>This almost worked, it breaks down if you have to support right to left text as the right to left control sequence always ends up in the middle of the placeholder.</p>
  
  <p><b>Sending Key Messages</b></p>
  
  <pre><code>public void AppendNewLine()
  {
      Keys[] keys = new Keys[] {Keys.Shift, Keys.Return};
      SendKeys(keys);
  }
  
  private void SendKeys(Keys[] keys)
  {
      foreach(Keys key in keys)
      {
          SendKeyDown(key);
      }
  }
  
  private void SendKeyDown(Keys key)
  {
      user32.SendMessage(this.Handle, Messages.WM_KEYDOWN, (int)key, 0);
  }
  
  private void SendKeyUp(Keys key)
  {
      user32.SendMessage(this.Handle, Messages.WM_KEYUP, (int)key, 0);
  }
  </code></pre>
  
  <p>This also ends up being converted to a \par</p>
  
  <p>Is there a way to post a messaged directly to the msftedit control to insert a control character?</p>
  
  <p>I am totally stumped, any ideas guys? Thanks for your help!</p>