如何在C#Winform中更改文本框中某些子字符串的字体颜色? [英] How to change the font color of some substring in the textbox in C# Winform?
本文介绍了如何在C#Winform中更改文本框中某些子字符串的字体颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我想将文本文件上传到文本框中,并想突出显示某些带有更改字体颜色的单词,我知道我需要编写 TextBox.ForeColor = Color.SomeColor;
但是,如果我希望并非所有文本都为相同颜色,则只有一些子字符串。
我该怎么做?
解决方案
您不能在TextBox控件中执行此操作,只能在RichTextBox控件中。
if(myRichTextBox.TextLenght> = 5)
{
myRichTextBox.Select(0,5);
myRichTextBox.SelectionColor = Color.Green;
}
if(myRichTextBox.TextLenght> = 15)
{
myRichTextBox.Select(10,15);
myRichTextBox.SelectionColor = Color.Red;
}
If I want to upload a text file into the textbox and want to highlight certain words with a font color change, i know that i need to write TextBox.ForeColor = Color.SomeColor;
But if i want that not all the text will be in the same color, only some Substrings.
How can I do that?
解决方案
Check the answer by Pieter Joost van de Sande.
You can't do this in the TextBox control, only in the RichTextBox control.
if( myRichTextBox.TextLenght >= 5 )
{
myRichTextBox.Select( 0, 5 );
myRichTextBox.SelectionColor = Color.Green;
}
if( myRichTextBox.TextLenght >= 15 )
{
myRichTextBox.Select( 10, 15 );
myRichTextBox.SelectionColor = Color.Red;
}
这篇关于如何在C#Winform中更改文本框中某些子字符串的字体颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文