C# 使用拆分后如何更改RichTextBox上确定字符串的字体颜色
你好,大师/程序员。C# 使用拆分后如何更改RichTextBox上确定字符串的字体颜色,c#,visual-studio-2010,C#,Visual Studio 2010,你好,大师/程序员。 我正在尝试使用Split(),使用它之后,我想检查RTB上的输入是否=我的点,然后在RTB上更改字体颜色。比如这个例子 在RTB上输入:切尔西是我最喜欢的足球俱乐部。我喜欢踢足球 我的观点:足球。 然后分割输入,然后在每个索引中检查分割结果。 最后,找到了ex:arr[4]和[9]=football 然后,如何在RTB屏幕上更改字体颜色,如“切尔西是我最喜欢的足球,我喜欢踢足球俱乐部” 下面是我的代码示例: ArrayList arrInput = new ArrayLi
我正在尝试使用Split(),使用它之后,我想检查RTB上的输入是否=我的点,然后在RTB上更改字体颜色。比如这个例子 在RTB上输入:
切尔西是我最喜欢的足球俱乐部。我喜欢踢足球
我的观点:
足球。
然后分割输入,然后在每个索引中检查分割结果。最后,找到了ex:
arr[4]和[9]=football
然后,如何在RTB屏幕上更改字体颜色,如“切尔西是我最喜欢的足球,我喜欢踢足球俱乐部”
下面是我的代码示例:
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?? Im confused. We know that football on arrInput[4].ToString() and [9]
//How to change font color on RTB screen when the input == football
您需要选择
football
并设置SelectionColor
属性
foreach (string s in splitString)
{
string trimmedS = s.Trim();
if (trimmedS != "")
{
int selectionStart = -1;
if (trimmedS.ToLower == "football") // Find the string you want to color
selectionStart = allInput.Count;
allInput.Add(s);
if (selectionStart != -1)
{
rtbInput.SelectionStart = selectionStart; // Select that string on your RTB
rtbInput.SelectionLength = trimmedS.Length;
rtbInput.SelectionColor = myCustomColor; // Set your color here
}
}
}
编辑:另类
// create your allInput first
int selectionStart = allInput.ToLower().IndexOf("football");
if (selectionStart != -1)
{
rtbInput.SelectionStart = selectionStart;
rtbInput.SelectionLength = ("football").Length;
rtbInput.SelectionColor = myCustomColor;
}
// create your allInput first
Regex regex = new Regex("football", RegexOptions.IgnoreCase); // using System.Text.RegularExpressions;
foreach (Match match in regex.Matches(allInput))
{
rtbInput.SelectionStart = match.Index;
rtbInput.SelectionLength = match.Length;
rtbInput.SelectionColor = myCustomColor;
}
我推荐第二个答案,因为我不确定在我们继续构建RichTextBox.Text时,颜色是否会保持不变
Edit2:
另一种选择
// create your allInput first
int selectionStart = allInput.ToLower().IndexOf("football");
if (selectionStart != -1)
{
rtbInput.SelectionStart = selectionStart;
rtbInput.SelectionLength = ("football").Length;
rtbInput.SelectionColor = myCustomColor;
}
// create your allInput first
Regex regex = new Regex("football", RegexOptions.IgnoreCase); // using System.Text.RegularExpressions;
foreach (Match match in regex.Matches(allInput))
{
rtbInput.SelectionStart = match.Index;
rtbInput.SelectionLength = match.Length;
rtbInput.SelectionColor = myCustomColor;
}
什么是“RTB屏幕”?甚至不知道…@RB我猜RTB代表RichTextBox
@Nolonar Ahhh!这是有道理的。我本该想到的…@All:有什么建议吗?@BerryHarahap我写了一个答案。希望这就是你想要的。诺洛纳先生,我试过了,但它只适用于第一场足球赛。喜欢输入:切尔西是最受欢迎的“足球”。我不会为下一个足球词汇而工作,比如“切尔西是我最喜欢的足球俱乐部。我喜欢踢足球”@BerryHarahap我添加了另一个替代方案,支持多场比赛。PS:完成所有子字符串的着色后,您始终可以清除所选内容(rtbInput.SelectionLength=0
)亲爱的@Nolonar。。(非常感谢。):)干杯