C# 当用户在richtextbox c中输入姓名时,如何给姓名列表上色#
我有一张名单,比如“约翰”、“多伊”。。等 我想在用户在richtextbox中输入一个或多个时给它们上色 如果用户删除了名字中的一个字母(如“john”),则表示该单词的颜色 恢复到原来的颜色C# 当用户在richtextbox c中输入姓名时,如何给姓名列表上色#,c#,C#,我有一张名单,比如“约翰”、“多伊”。。等 我想在用户在richtextbox中输入一个或多个时给它们上色 如果用户删除了名字中的一个字母(如“john”),则表示该单词的颜色 恢复到原来的颜色 列表名称=新列表 { “约翰”, “能源部”, “杰克”, “莉莎”, “桑迪”, “莎拉” }; 私有void richTextBox1_TextChanged(对象发送方,事件参数e) { foreach(名称中的字符串名称) { if(richTextBox1.Text.Contains(nam
列表名称=新列表
{
“约翰”,
“能源部”,
“杰克”,
“莉莎”,
“桑迪”,
“莎拉”
};
私有void richTextBox1_TextChanged(对象发送方,事件参数e)
{
foreach(名称中的字符串名称)
{
if(richTextBox1.Text.Contains(name))
{
var matchstring=Regex.Escape(名称);
foreach(在Regex.Matches(richTextBox1.Text,matchstring)中匹配)
{
richTextBox1.Select(match.Index,match.Length);
richTextBox1.SelectionColor=Color.Red;
richTextBox1.选择(richTextBox1.TextLength,0);
richTextBox1.SelectionColor=richTextBox1.ForeColor;
}
}
其他的
{
richTextBox1.SelectAll();
richTextBox1.SelectionColor=Color.Black;
richTextBox1.选择(richTextBox1.TextLength,0);
}
}
}
以下是我上述评论的一个例子:
private const int WM_SETREDRAW = 0xB;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
List<string> names = new List<string>
{
"john",
"doe",
"jack",
"liza",
"sandy",
"sara"
};
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
SendMessage(richTextBox1.Handle, WM_SETREDRAW, false, 0);
int prevStart = richTextBox1.SelectionStart;
int prevLength = richTextBox1.SelectionLength;
richTextBox1.SelectAll();
richTextBox1.SelectionColor = Color.Black;
foreach (string name in names)
{
foreach (Match match in Regex.Matches(richTextBox1.Text, Regex.Escape(name)))
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionColor = Color.Red;
}
}
richTextBox1.Select(prevStart, prevLength);
SendMessage(richTextBox1.Handle, WM_SETREDRAW, true, 0);
richTextBox1.Invalidate();
}
private const int WM_SETREDRAW=0xB;
[System.Runtime.InteropServices.DllImport(“user32.dll”)]
公共静态外部int SendMessage(IntPtr hWnd、Int32 wMsg、bool wParam、Int32 lParam);
列表名称=新列表
{
“约翰”,
“能源部”,
“杰克”,
“莉莎”,
“桑迪”,
“莎拉”
};
私有void richTextBox1_TextChanged(对象发送方,事件参数e)
{
SendMessage(richTextBox1.Handle,WM_SETREDRAW,false,0);
int prevStart=richTextBox1.SelectionStart;
int prevLength=richTextBox1.SelectionLength;
richTextBox1.SelectAll();
richTextBox1.SelectionColor=Color.Black;
foreach(名称中的字符串名称)
{
foreach(Regex.Matches(richTextBox1.Text,Regex.Escape(name))中的匹配)
{
richTextBox1.Select(match.Index,match.Length);
richTextBox1.SelectionColor=Color.Red;
}
}
richTextBox1.选择(prevStart,prevLength);
SendMessage(richTextBox1.Handle,WM_SETREDRAW,true,0);
richTextBox1.Invalidate();
}
*WM_SETREDRAW调用对于在更新RichTextBox时减少闪烁是必要的。如果没有它们,随着文本变得越来越长,闪烁变得非常明显。在“john”中没有删除的字母。那么为什么john的颜色是黑色而不是红色?通过这种方法,如果文本包含“john”,而不是“doe”,会发生什么?首先,“john”将变为红色,但当找不到“doe”时,所有文本将再次变为黑色。要解决此问题,请将所有文本设置为黑色(在循环外部和之前),然后在循环中仅将匹配项更改为红色。
private const int WM_SETREDRAW = 0xB;
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, Int32 wMsg, bool wParam, Int32 lParam);
List<string> names = new List<string>
{
"john",
"doe",
"jack",
"liza",
"sandy",
"sara"
};
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
SendMessage(richTextBox1.Handle, WM_SETREDRAW, false, 0);
int prevStart = richTextBox1.SelectionStart;
int prevLength = richTextBox1.SelectionLength;
richTextBox1.SelectAll();
richTextBox1.SelectionColor = Color.Black;
foreach (string name in names)
{
foreach (Match match in Regex.Matches(richTextBox1.Text, Regex.Escape(name)))
{
richTextBox1.Select(match.Index, match.Length);
richTextBox1.SelectionColor = Color.Red;
}
}
richTextBox1.Select(prevStart, prevLength);
SendMessage(richTextBox1.Handle, WM_SETREDRAW, true, 0);
richTextBox1.Invalidate();
}