17

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

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
Johnston
  • 2,873
  • 8
  • 29
  • 39
  • 1
    Hmmm,I don't know if this will help you but in S.W.F(System.Windows.Forms) namespace threre is the RichTextBox class that has the SelectionFont propeerty.I don't know if it will work with WPF though. –  Jun 19 '11 at 20:52

7 Answers7

13

use a RichTextBox, below a method that i have wrote for this problem - hope it helps ;-)

/// <summary>
/// This method highlights the assigned text with the specified color.
/// </summary>
/// <param name="textToMark">The text to be marked.</param>
/// <param name="color">The new Backgroundcolor.</param>
/// <param name="richTextBox">The RichTextBox.</param>
/// <param name="startIndex">The zero-based starting caracter position.</param>
public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox, int startIndex)
{
    if (startIndex < 0 || startIndex > textToMark.Length-1) startIndex = 0;

    System.Drawing.Font newFont = new Font("Verdana", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 178, false);
    try
    {               
        foreach (string line in richTextBox.Lines)
        { 
            if (line.Contains(textToMark))
            {
                richTextBox.Select(startIndex, line.Length);
                richTextBox.SelectionBackColor = color;
            }
            startIndex += line.Length +1;
        }
    }
    catch
    { }
}
jwillmer
  • 3,570
  • 5
  • 37
  • 73
12

You can use TextBlock with other TextBlocks or Runs inside:

<TextBlock>
    normal text
    <TextBlock FontWeight="Bold">bold text</TextBlock>
    more normal text
    <Run FontWeight="Bold">more bold text</Run>
</TextBlock>
svick
  • 236,525
  • 50
  • 385
  • 514
11

You will need to use a RichTextBox to achieve this:

<RichTextBox Name="richTB">
  <FlowDocument>
    <Paragraph>
      <Run FontWeight="Bold">Your Name:</Run>
      <Run Text="{Binding NameProperty}"/>
    </Paragraph>
  </FlowDocument>
</RichTextBox>

But why would you want "Your Name" to be editable? Surely you would want it as a separate, readonly, label?

<StackPanel Orientation="Horizontal">
    <Label FontWeight="Bold">Your Name:</Label>
    <TextBox Text="{Binding NameProperty}"/>
</StackPanel>
lordnik22
  • 48
  • 1
  • 8
Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
4

A regular TextBox only supports the all or nothing setting of such stylistic properties. You might want to look into RichTextBox, though, you can't just specify a set of values for a Text property in the way you have tried - you will need to work with a FlowDocument to construct your text body through the Document property.

For an overview of working with a FlowDocument, and some examples, give this a read.

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
  • so im trying a basic flowDocument in a rich text box and it doesnt seem to do anything -- im using the example from msdna: http://msdn.microsoft.com/en-us/library/ms597536.aspx – Johnston Jun 19 '11 at 17:49
1

Have a look at the RichTextBox Control it basically works the same as the TextBox but allows for more customization and takes, of course, Rich Text which allows for partial formatting..

PedroC88
  • 3,708
  • 7
  • 43
  • 77
0

Taking jwillmer's excellent example, I made some adjustments because it was coloring the entire error line for me:

    public static void ChangeTextcolor(string textToMark, Color color, RichTextBox richTextBox)
    {
        int startIndex = 0;

        string text = richTextBox.Text;
        startIndex = text.IndexOf(textToMark);

        System.Drawing.Font newFont = new Font("Verdana", 10f, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 178, false);

        try
        {
            foreach (string line in richTextBox.Lines)
            {
                if (line.Contains(textToMark))
                {
                    richTextBox.Select(startIndex, textToMark.Length);
                    richTextBox.SelectionColor = color;
                    richTextBox.SelectionFont = newFont;
                }
            }
        }
        catch{ }
    }

Also, I added unique tags before and after the text to color to get the text, then removed them.

Zath
  • 547
  • 2
  • 10
  • 25
0

jwillmer's answer had a few errors for me. These were solved by adding:

using System.Drawing;

and then changing the inputs to:

public static void ChangeTextcolor(string textToMark, System.Drawing.Color color, System.Windows.Forms.RichTextBox richTextBox, int startIndex)

This was because my code was looking for System.Windows.Controls.RichTextbox not Windows.Forums.RichTextBox. And System.Windows.Media.Color not System.Drawing.Color

Failed Scientist
  • 1,977
  • 3
  • 29
  • 48
Kieron
  • 3
  • 3