问题描述
我目前有一个 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 中使特定文本加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!