【问题标题】:Making specific Text Boldefaced in a TextBox在 TextBox 中使特定文本加粗
【发布时间】:2018-06-25 18:32:47
【问题描述】:

嗨,我目前有一个 texbox,当用户按下不同的按钮时,它会向用户打印信息。我想知道是否有办法让我的部分文本只加粗,而其余部分不加粗。

我尝试了以下方法:

textBox1.FontWeight = FontWeights.UltraBold;
textBox1.Text. = ("Your Name: " );
TextBox1.FontWeight = FontWeights.Regular;
textBox1.Text += (nameVar);

唯一的问题是,使用这种方式要么使所有内容变得粗体,要么什么都没有。 有没有办法做到这一点?我在 C# 中使用 WPF 项目

任何意见或建议表示赞赏。 谢谢!

编辑:所以现在我正在尝试执行你们都建议的 RichText 框,但我似乎无法在其中显示任何内容:

// 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 是我在 wpf 中创建的富文本框的名称

谢谢

【问题讨论】:

  • 嗯,我不知道这是否会对您有所帮助,但在 S.W.F(System.Windows.Forms) 命名空间中,有具有 SelectionFont 属性的 RichTextBox 类。我不知道它是否会不过可以使用 WPF。

标签: c# wpf textbox bold


【解决方案1】:

在我为这个问题编写的方法下方使用 RichTextBox - 希望它有所帮助;-)

/// <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
    { }
}

【讨论】:

    【解决方案2】:

    您可以将TextBlock 与其他TextBlocks 或Runs 一起使用:

    <TextBlock>
        normal text
        <TextBlock FontWeight="Bold">bold text</TextBlock>
        more normal text
        <Run FontWeight="Bold">more bold text</Run>
    </TextBlock>
    

    【讨论】:

    • 如何以编程方式做到这一点?
    • @digz6666 将它们添加到Inlines 集合中。
    【解决方案3】:

    您需要使用RichTextBox 来实现此目的:

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

    但您为什么希望“您的名字”可编辑?您肯定希望它作为单独的只读标签吗?

    <StackPanel Orientation="Horizontal">
        <Label FontWeight="Bold">Your Name:</Label>
        <TextBox Text="{Binding NameProperty}"/>
    </StackPanel>
    

    【讨论】:

      【解决方案4】:

      常规的TextBox 仅支持此类样式属性的all or nothing 设置。您可能想查看RichTextBox,但是,您不能以您尝试过的方式为Text 属性指定一组值 - 您需要使用FlowDocument 来构建您的文本正文通过Document 属性。

      有关使用FlowDocument 的概述以及一些示例,give this a read

      【讨论】:

      【解决方案5】:

      看看RichTextBox Control,它的工作原理与TextBox 基本相同,但允许进行更多自定义,当然还有Rich Text,它允许部分格式化..

      【讨论】:

        【解决方案6】:

        以 jwillmer 的出色示例为例,我进行了一些调整,因为它为我着色了整个错误线:

            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{ }
            }
        

        另外,我在文本前后添加了独特的标签来着色以获取文本,然后将它们删除。

        【讨论】:

          【解决方案7】:

          jwillmer 的回答对我来说有一些错误。这些通过添加解决:

          using System.Drawing;
          

          然后将输入更改为:

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

          这是因为我的代码在寻找 System.Windows.Controls.RichTextbox 而不是 Windows.Forums.RichTextBox。而System.Windows.Media.Color 不是System.Drawing.Color

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-12-24
            • 2022-07-10
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多