There are many examples of how to format the text in a TextBox via xaml code, but I am trying to figure out how to change the code in my .cs file.
//TextBox tb initialized in .xaml code
tb.Text = "<bold>Bold</bold> and normal and <italic>Italic</italic>";
is along the lines of what I am looking for. Is this possible?
The end result would look like:
Bold and normal and Italic
You can do that for RichTextBox by adding Inlines like this:
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Bold(new Run("Bold")));
paragraph.Inlines.Add(new Run(" and normal"));
paragraph.Inlines.Add(new Italic(new Run(" and Italic")));
richTextBox.Document = new FlowDocument(paragraph);
If you decide to use TextBlock you can use the following:
this.myTextBlock.Inlines.Add(new Bold(new Run("Bold")));
this.myTextBlock.Inlines.Add(" and normal and ");
this.myTextBlock.Inlines.Add(new Italic(new Run("italic")));
Otherwise, if you have to use TextBox you can only apply style to whole text, for example using myTextBox.FontWeight.
Related
So i am currently writing a small IRC bot for Twitch, and i am doing it with WPF, and i would like to color only username in the text line i add to richTextBox. I tryed with simple Foreground coloring but it colors me everytime everything.
My current code:
if (e.ChatMessage.ColorHex.StartsWith("#"))
{
richTextBox.Foreground = ChatUtils.convertHexToBrush(e.ChatMessage.ColorHex);
}
richTextBox.AppendText(String.Format("[{0}] <{1}>: {2}",
DateTime.Now.ToString("HH:mm:ss"),
e.ChatMessage.DisplayName, e.ChatMessage.Message) + "\n");
richTextBox.ScrollToEnd();
So how would i color only the parameter {1} which is e.ChatMessage.DisplayName?
Try like this-
TextRange tr = new TextRange(rtb.Document.ContentEnd, rtb.Document.ContentEnd);
tr.Text = e.ChatMessage.DisplayName;
tr.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);
See if this helps.
This question already has answers here:
Change color and font for some part of text in WPF C#
(5 answers)
Closed 2 years ago.
Good day!
I try to change part of text to red color.
So, i try to use TextBox, but it not works.
So, i read, that RichTextBox can do that:i use this question
But i do not know how to append colored text?
TextRange rangeOfText1 = new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);
rangeOfText1.Text = "Text1 ";
rangeOfText1.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue);
rangeOfText1.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
Ok, i get TextRange, but how to append it to RichTextBox?
Can you tell me how to make some part of text to red color?
Thank you!
Starting with your example:
TextRange rangeOfText2 = new TextRange(tbScriptCode.Document.ContentEnd,
tbScriptCode.Document.ContentEnd);
rangeOfText2.Text = "RED !";
rangeOfText2.ApplyPropertyValue(TextElement.ForegroundProperty,Brushes.Red);
rangeOfText2.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
works for me.
Ok, i get TextRange, but how to append it to RichTextBox?
It has already been added with
new TextRange(tbScriptCode.Document.ContentEnd, tbScriptCode.Document.ContentEnd);
What you also can do (I used this myself lately):
var fd = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run("normal text and this is in "));
paragraph.Inlines.Add(new Run("red") { Foreground = Brushes.Red });
paragraph.Inlines.Add(new Run(" and this is blue.") { Foreground = Brushes.Blue });
fd.Blocks.Add(paragraph);
tbScriptCode.Document = fd;
I am new to WPF's RichTextBox. I would like to know how to highlight text on a line with a specific colour.
Let's say I have a rich text box with a yellow background and assign to it a flow document.
richTextBox.Background = Brushes.LightYellow;
var mcFlowDoc = new FlowDocument();
var para = new Paragraph();
para.Inlines.Add(new Run("This is the first line.\n"));
para.Inlines.Add(new Run("This is the second line.\n"));
para.Inlines.Add(new Run("This is the third line."));
mcFlowDoc.Blocks.Add(para);
richTextBox.Document = mcFlowDoc;
What would I have to do next to change the third line's highlight colour to red? I am not talking about selection highlight colour, but normal text highligting (like in WordPad)
If there is a solution, I would like it in C# code, I want to stay away from XAML editing.
Run run = new Run("Red is the third line.\n");
// run.Foreground = Brushes.Red;
run.Background = Brushes.Red;
para.Inlines.Add(run);
StringBuilder sb = new StringBuilder();
sb.Append(
string.Format("{0} |{1} ", Name, Value)
);
Display.Text = sb.ToString(); // Display is a WP7 TextBlock control
I want to make "Name" as bold. Is it possible to do that ?
ChrisF offers the RichTextBox as a solution but its less well known that simple font variation is acheivable with the simple TextBlock:-
myTextBlock.Inlines.Add(new Run() { Text = "Hello " });
myTextBlock.Inlines.Add(new Run() { Text = "World", FontWeight= FontWeights.Bold });
A StringBuilder only contains character data, not formatting. You can't, basically. Unless you are actually generating html or rtf etc.
In the same way that notepad.exe doesn't have bold/italics/etc.
I'm not a WP7 expert, but maybe there is a different control you can use here, more aimed at formatted text.
You'll need to put the text into a RichTextBox and have the name as a separate Run in the Paragraph as in this example from the MSDN:
// Create a Run of plain text and some bold text.
Run myRun1 = new Run();
myRun1.Text = "A RichTextBox with ";
Bold myBold = new Bold();
myBold.Inlines.Add("initial content ");
Run myRun2 = new Run();
myRun2.Text = "in it.";
// Create a paragraph and add the Run and Bold to it.
Paragraph myParagraph = new Paragraph();
myParagraph.Inlines.Add(myRun1);
myParagraph.Inlines.Add(myBold);
myParagraph.Inlines.Add(myRun2);
// Add the paragraph to the RichTextBox.
MyRTB.Blocks.Add(myParagraph);
I have a WPF RichTextBox that is dynamically built in a WPF web service. This web service accepts a xaml string that is extracted from the contents of a third-party silverlight RichTextBox control.
<Paragraph TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>
How do I insert this xaml into my WPF RichTextBox? I somewhat understand the concepts of the FlowDocument and Paragraph and Run so I can populate the WPF RichTextBox with text using the code below,
FlowDocument flowDocument = new FlowDocument();
Paragraph par = new Paragraph();
par.FontSize = 16;
par.FontWeight = FontWeights.Bold;
par.Inlines.Add(new Run("Paragraph text"));
flowDocument.Blocks.Add(par);
rtb.Document = flowDocument;
But what I really don't want to have to parse through the xaml myself to build a paragraph as it can get very complicated. Is there a way to just have the control know how to parse the passed in xaml?
You can use XamlReader to read your Xaml string and convert it to a control:
string templateString = "<Paragraph xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" TextAlignment=\"Left\"><Run FontFamily=\"Comic Sans MS\" FontSize=\"16\" Foreground=\"#FF0000FF\" FontWeight=\"Bold\" >This text is blue and bold.</Run></Paragraph>";
StringReader stringReader = new StringReader(templateString);
XmlReader xmlReader = XmlReader.Create(stringReader);
Paragraph template = (Paragraph)XamlReader.Load(xmlReader);
Just make sure you include the following tag in your template:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
HTH