title: Writing RTF to RichEditBox
body: <p>I'm writing a program using WinRT and have a RichEditBox which I'm struggling to make behave the way I want.</p>
  <p>The idea is that a user will type some input, the program will process this to some output, and put it on the next line, centred, and then await input on the next line, back left aligned.</p>
  <p>I figured that to do this I need to read the box content, cut off the last part of the RTF syntax, add the output, and then put the syntax back. This mostly works but sometimes it gives two blank lines and then the output left-aligned.</p>
  <p>Can anyone help?</p>
  <p>Here's my function to write the output.</p>
  <pre><code>void MainPage::WriteOutput(std::wstring const&amp; output)
      {
          hstring h_RTF;
          hstring h_plain;
          MainBox().Document().GetText(Text::TextGetOptions::FormatRtf, h_RTF);
  
          //Get plain text version to help seperate RTF syntx
          MainBox().Document().GetText(Text::TextGetOptions::None, h_plain);
  
          //Convert hstring to wstring, remove new line characters from end of plain
          std::wstring RTF = h_RTF.c_str();
          std::wstring plain = h_plain.c_str();
          plain.pop_back();
          plain.pop_back();
  
          //seperate last line
          auto found = plain.find_last_of(L'\r');
          if (found != std::wstring::npos)
              plain = plain.substr(found + 1);
  
          int searchLength = std::min&lt;int&gt;(plain.length(), 5);
          std::wstring searchString = plain.substr(plain.length() - searchLength);
  
          //find last line in RTF
          found = RTF.find_last_of(searchString);
          if (found == std::wstring::npos)
              throw(&quot;String not found&quot;);
  
          //cut up to end of last line
          std::wstring before = RTF.substr(0, found + 1);
          //cut after end of last line
          std::wstring after = RTF.substr(found + 5);
          //new stuff to add
          std::wstring extra = L&quot;\\par\\pard\\qc &quot; + output;
          //add strings to create new content
          MainBoxContent = before + extra + after;
          //put content in box
          SetMainBoxContent();
          MainBox().Focus(FocusState::Programmatic);                
     MainBox().TextDocument().Selection().EndOf(Text::TextRangeUnit::Window,false);
      }
  </code></pre>