Making specific Text Boldefaced in a TextBox

前端 未结 7 1945
情歌与酒
情歌与酒 2020-11-28 12:37

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

相关标签:
7条回答
  • 2020-11-28 13:09

    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>
    
    0 讨论(0)
  • 2020-11-28 13:11

    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..

    0 讨论(0)
  • 2020-11-28 13:14

    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>
    
    0 讨论(0)
  • 2020-11-28 13:15

    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.

    0 讨论(0)
  • 2020-11-28 13:27

    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
        { }
    }
    
    0 讨论(0)
  • 2020-11-28 13:27

    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

    0 讨论(0)
提交回复
热议问题