在文本框中将特定文本设为粗体

发布于 2024-11-16 04:53:07 字数 806 浏览 8 评论 0原文

您好,我目前有一个文本框,当用户按下不同的按钮时,它会向用户打印信息。我想知道是否有一种方法可以只将部分文本加粗,而其余文本则不加粗。

我尝试过以下方法:

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 中创建的 Richtextbox 的名称

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

心舞飞扬 2024-11-23 04:53:07

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

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
    { }
}
删除会话 2024-11-23 04:53:07

您可以将 TextBlock 与其他 TextBlockRun 一起使用:

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

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>
梦萦几度 2024-11-23 04:53:07

您将需要使用 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>

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>
你怎么敢 2024-11-23 04:53:07

常规 TextBox 仅支持此类样式属性的“全有或全无”设置。您可能需要查看 RichTextBox,但是,您不能只按照您尝试过的方式为 Text 属性指定一组值 - 您将需要使用 FlowDocument 构建您的通过 文档 属性。

有关使用 FlowDocument 的概述和一些示例,读一读

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.

东北女汉子 2024-11-23 04:53:07

查看 RichTextBox 控件 它的工作原理基本上与 TextBox 相同,但允许更多自定义,当然,富文本 允许部分格式化..

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

街角迷惘 2024-11-23 04:53:07

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

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

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.

素手挽清风 2024-11-23 04:53:07

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文