在 TextBox 中使特定文本加粗

Making specific Text Boldefaced in a TextBox(在 TextBox 中使特定文本加粗)
本文介绍了在 TextBox 中使特定文本加粗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我尝试了以下方法:

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

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

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!

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

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

rtb is the name of my richtextbox i created in my wpf

谢谢

推荐答案

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

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

这篇关于在 TextBox 中使特定文本加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)