Making specific Text Boldefaced in a 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 中使特定文本加粗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 TextBox 中使特定文本加粗


- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01
- 使用 rss + c# 2022-01-01
- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01