如何在使用SPLIT后更改RichTextBox上的字体颜色 [英] How to Change Font Color on RichTextBox after USING SPLIT
问题描述
Hello Master / Programmer。
我正在尝试使用Split(),在使用它之后,我想检查一下RTB上的输入==我的点然后改变字体颜色在RTB上。就像这个例子一样。
输入RTB:切尔西是我最喜欢的足球俱乐部。我喜欢踢足球
我的观点:足球。
然后我拆分输入,然后我检查arr每个索引的拆分结果。
最后,找到ex: arr [4]和[9] =足球
然后,如何在RTB屏幕上更改字体颜色,如切尔西是我最喜欢的 football
。我喜欢玩 football
俱乐部。?
这是我的代码示例:
Hello Master / Programmer.
I''m trying to use Split(), after using it, i wanna check if the input on RTB == my point then change font color on RTB. Like this example.
INPUT on RTB : Chelsea is my favorite football club. I like playing football
My point : football.
Then I split the input, then i check the result of split in arr each index.
Finally, found ex : arr[4] and [9] = football
Then, how to change font color on RTB screen like
"Chelsea is my favorite football
. I like playing football
club."?
This my code example :
ArrayList arrInput = new ArrayList();
//if the input Chelsea is my favorite football club. I like playing football
string allInput = rtbInput.Text;
string[] splitString = allInput.Split(new char[] { ' ', '\t', ',', '.'});
foreach (string s in splitString)
{
if (s.Trim() != "")
{
int selectionStart = s.ToLower().IndexOf("football");
if (selectionStart != -1)
{
rtbInput.SelectionStart = selectionStart;
rtbInput.SelectionLength = ("football").Length;
rtbInput.SelectionColor = Color.Red;
}
}
//What Next?? We know that football on arrInput[4].ToString() and [9]
//How to change font color on RTB screen when the input == football
时,如何在RTB屏幕上更改字体颜色
推荐答案
这里有一些问题。
有一个未使用的变量,
Hi,
There are few things wrong here.
There is an unused variable,
ArrayList arrInput = new ArrayList();
我不知道为什么你有这个变量。
这个字符串被分成单词然后预约单词a寻找单词football并得到该单词的索引
I am not sure why you have this variable for.
The string has been split into words and then foreach word a looking for the word "football" and getting the index of that word
int selectionStart = s.ToLower().IndexOf("football");
这将始终将selectionSrart返回为0 ;因为你没有在整个信号中寻找索引而只是那个词。
所以在上面的代码中,只有前8个字符将是你设置的颜色,但是你想要的实际文字不会是彩色的。
你需要的是:
This will always return selectionSrart as 0; because you not looking for an index in the whole sentance instead just that word.
So in your code above, only the first 8 characters will be in the colour you set, but what you actual text you wanted wouldn''t be in colour.
What you need is:
int index = 0;
while (index != -1)
{
// on the first round the index will be zero, thereafter the index will not be zero
// therefore the index needs to be incremented by 1 from the last index so it can continue
// searching, otherwise the search will stuck to that same index.
index = str.IndexOf(rtbInput.Text, (index != 0) ? (index + 1) : index );
if ( index != -1)
{
rtbInput.SelectionStart = index;
rtbInput.SelectionLength = ("football").Length;
rtbInput.SelectionColor = Color.Red;
}
}
我希望这会有所帮助。
问候
Jegan
I hope this helps.
Regards
Jegan
这篇关于如何在使用SPLIT后更改RichTextBox上的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!