Answered by:
How to add lines of text with different kind of colors in a RichTextBox

Question
-
Hi All,
Please can someone show me how I can programmatically add lines (sentence) in different kinds of colors in a RichTextBox?
Just like in a chat, I have TextBox where I can type text in, when clicking on the send button the text what is typed in the TextBox must appear in the RichText and with a color.
Example:
Xaml:
<Grid Background="{StaticResource ApplicationPageBackgroundBrush}"> <RichEditBox x:Name="myRichEditBox" HorizontalAlignment="Left" Margin="86,83,0,0" VerticalAlignment="Top" Height="234" Width="388"/> </Grid>
Code:
void BlankPage::SomeFunction(void) { String^ str1 = "This text line is red\n"; // How to add this line of text into the RichTextBox with color red? String^ str2 = "This text line is blue\n"; // How to add this line of text into the RichTextBox with color blue? String^ str3 = "This text line is green\n"; // How to add this line of text into the RichTextBox with color green? // How to add the text above with the right color into the RichTextBox? myRichEditBox->...............? }
I have changed my question; added some example code. Hopefully it's more clear with what I mean, please can someone give an example how to add these strings into the RichEditBox with the right colors?
Oh before I forget; This RichEditBox accepts returns (multiline), so the text must appear underneath each other, not replacing one text with the other.
- Edited by ArtoriusCPP Tuesday, April 10, 2012 10:40 AM
Sunday, April 8, 2012 7:06 PM
Answers
-
RichTextBlock and RichEditBox do similar things, but aren't actually related controls. If you don't need the text to be editable then RichTextBlock is probably better to use. RichEditBox is good if you need the rich text to be edited. For a chat log I would probably use a RichTextBlock.
For a RichEditBox, you can access its Text Object Model via the RichEditBox::Document property. From there you can get the Selection, set it to the end, set the text in the selection, and set the selection's CharacterFormat (including color).
For a RichTextBlock, you can create Block objects (such as Paragraphs), set them up, and then add them to the RichTextBox's Blocks collection.
Here's a quick sample of each:
std::wstring strings[] = { L"this line is red\r\n", L"this line is green\r\n", L"this line is blue\r\n" }; Windows::UI::Color colors[] = { Colors::Red, Colors::Green, Colors::Blue }; for(int i=0;i<3; i++) { reb->Document->Selection->EndKey(TextRangeUnit::Story,false); reb->Document->Selection->SetText(TextSetOptions::None,ref new String(strings[i].c_str())); reb->Document->Selection->CharacterFormat->BackgroundColor = colors[i]; } for(int i=0;i<3; i++) { Paragraph^ par = ref new Paragraph(); par->Foreground = ref new SolidColorBrush(colors[i]); Run^ run = ref new Run(); run->Text = ref new String(strings[i].c_str()); par->Inlines->Append(run); rtb->Blocks->Append(par); }
--Rob
- Proposed as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Wednesday, April 11, 2012 4:02 AM
- Marked as answer by ArtoriusCPP Wednesday, April 11, 2012 7:42 AM
Wednesday, April 11, 2012 4:02 AMModerator
All replies
-
Try this RichTextBlock.Foreground http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.richtextblock.foreground.aspx
NEU_ShieldEdge
Monday, April 9, 2012 12:03 PM -
Try this RichTextBlock.Foreground http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.richtextblock.foreground.aspx
NEU_ShieldEdge
Thanks for your replay; I already have read this library topic, but how to use this property? To set the color programmatically in C++ not in XAML for the line of text that needs to me added into a RichTextBox.<o:p></o:p>
Tuesday, April 10, 2012 10:18 AM -
RichTextBlock and RichEditBox do similar things, but aren't actually related controls. If you don't need the text to be editable then RichTextBlock is probably better to use. RichEditBox is good if you need the rich text to be edited. For a chat log I would probably use a RichTextBlock.
For a RichEditBox, you can access its Text Object Model via the RichEditBox::Document property. From there you can get the Selection, set it to the end, set the text in the selection, and set the selection's CharacterFormat (including color).
For a RichTextBlock, you can create Block objects (such as Paragraphs), set them up, and then add them to the RichTextBox's Blocks collection.
Here's a quick sample of each:
std::wstring strings[] = { L"this line is red\r\n", L"this line is green\r\n", L"this line is blue\r\n" }; Windows::UI::Color colors[] = { Colors::Red, Colors::Green, Colors::Blue }; for(int i=0;i<3; i++) { reb->Document->Selection->EndKey(TextRangeUnit::Story,false); reb->Document->Selection->SetText(TextSetOptions::None,ref new String(strings[i].c_str())); reb->Document->Selection->CharacterFormat->BackgroundColor = colors[i]; } for(int i=0;i<3; i++) { Paragraph^ par = ref new Paragraph(); par->Foreground = ref new SolidColorBrush(colors[i]); Run^ run = ref new Run(); run->Text = ref new String(strings[i].c_str()); par->Inlines->Append(run); rtb->Blocks->Append(par); }
--Rob
- Proposed as answer by Rob Caplan [MSFT]Microsoft employee, Moderator Wednesday, April 11, 2012 4:02 AM
- Marked as answer by ArtoriusCPP Wednesday, April 11, 2012 7:42 AM
Wednesday, April 11, 2012 4:02 AMModerator -
Hi Rob,
Yeah, thanks, this is what I have been looking for. It’s all clear now.
Greets,
Arthur
Wednesday, April 11, 2012 7:41 AM