Reputation: 41
I'm writing a program using WinRT and have a RichEditBox which I'm struggling to make behave the way I want.
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.
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.
Can anyone help?
Here's my function to write the output.
void MainPage::WriteOutput(std::wstring const& 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<int>(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("String not found");
//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"\\par\\pard\\qc " + 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);
}
Upvotes: 0
Views: 69