Hi I currently have a texbox that prints out info to the user when they press diffrent buttons. I was wondering if there was a way to make only some of my text bolded while the rest isnt.
Ive tried the following:
textBox1.FontWeight = FontWeights.UltraBold;
textBox1.Text. = ("Your Name: " );
TextBox1.FontWeight = FontWeights.Regular;
textBox1.Text += (nameVar);
Only problem is that using this way will either make everything bold or nothing. Is there a way to do this? Im using WPF project in C#
Any Comments or suggestions are appreciated. Thanks!
EDIT: So now im trying to do the RichText box that you all suggested but I cant seem to get anything to appear in it:
// Create a simple FlowDocument to serve as the content input for the construtor.
FlowDocument flowDoc = new FlowDocument(new Paragraph(new Run("Simple FlowDocument")));
// After this constructor is called, the new RichTextBox rtb will contain flowDoc.
RichTextBox rtb = new RichTextBox(flowDoc);
rtb is the name of my richtextbox i created in my wpf
Thanks
Taking jwillmer's excellent example, I made some adjustments because it was coloring the entire error line for me:
Also, I added unique tags before and after the text to color to get the text, then removed them.
You can use
TextBlock
with otherTextBlock
s orRun
s inside:A regular
TextBox
only supports the all or nothing setting of such stylistic properties. You might want to look intoRichTextBox
, though, you can't just specify a set of values for aText
property in the way you have tried - you will need to work with aFlowDocument
to construct your text body through theDocument
property.For an overview of working with a
FlowDocument
, and some examples, give this a read.jwillmer's answer had a few errors for me. These were solved by adding:
and then changing the inputs to:
This was because my code was looking for
System.Windows.Controls.RichTextbox
notWindows.Forums.RichTextBox
. AndSystem.Windows.Media.Color
notSystem.Drawing.Color
You will need to use a
RichTextBox
to achieve this:But why would you want "Your Name" to be editable? Surely you would want it as a separate, readonly, label?
use a RichTextBox, below a method that i have wrote for this problem - hope it helps ;-)