int a = 0;
string[] kc = new string[40] { "private","protected","public","namespace","class","object","if","else",
"while","switch","case","using","eventargs","return","null","void","int",
"string","float","char","this","set","new","true","false","const",
"static","internal","extends","super","import","default","break",
"try","catch","finally","main","writeline","console","writeLine" }; private void timer1_Tick(object sender, EventArgs e)
{
a = changeColor(kc);
timer1.Enabled = false;
}
/// <summary>
/// 改变richTextBox中指定字符串的颜色
/// 调用即可
/// </summary>
/// <param name="str" value="为指定的字符串"></param> public int changeColor(string[] str)
{
ArrayList list = null;
int b = 0;
for (int i = 0; i < str.Length; i++)
{
list = getIndexArray(richTextBox1.Text.ToLower(), str[i]);
b += list.Count;
}
for (int i = 0; i < str.Length; i++)
{
list = getIndexArray(richTextBox1.Text.ToLower(), str[i]);
if (list.Count == 0)
{
continue;
}
if (a == b)
{
richTextBox1.SelectionColor = Color.Empty;
return b;
}
for (int j = 0; j < list.Count; j++)
{
int index = (int)list[j];
richTextBox1.Select(index, str[i].Length);
richTextBox1.SelectionColor = Color.Blue;
this.richTextBox1.Focus();
//设置光标的位置到文本尾
this.richTextBox1.Select(this.richTextBox1.TextLength, 0);
//滚动到控件光标处
this.richTextBox1.ScrollToCaret();
richTextBox1.SelectionColor = Color.Empty;
}
}
return b;
} public ArrayList getIndexArray(String inputStr, String findStr)
{
ArrayList list = new ArrayList();
int start = 0;
while (start < inputStr.Length)
{
int index = inputStr.IndexOf(findStr, start);
if (index >= 0)
{
list.Add(index);
start = index + findStr.Length;
}
else
{
break;
}
}
return list;
} private void textBox1_TextChanged(object sender, EventArgs e)
{
timer1.Enabled = true;
}

  

RichText实现动态输入关键字高亮颜色显示的更多相关文章

  1. IOS_SearchBar搜索栏及关键字高亮

    搜索框的效果演示: 这个就是所谓的搜索框了,那么接下来我们看看如何使用代码来实现这个功能. 我所使用的数据是英雄联盟的英雄名单,是一个JSON数据的txt文件, JSON数据的处理代码如下所示: ? ...

  2. 在 Angular 中实现搜索关键字高亮

    在 Angular 中,我们不应该试图直接修改 DOM 的内容,当需要更新 DOM 内容的时候,应该修改的其实是我们的数据模型,也就是 $scope 中的数据,Angular 会帮助我们将修改之后的数 ...

  3. C#,WPF中使用多文本显示数据,并对其数据进行关键字高亮等操作

    需求:针对多文本信息显示,我们需要对其内容中的某些关键字或者某行进行高亮显示,并用不同颜色显示. 分析:在C#中,首先要进行多文本信息显示,可以RichTextBox(不要使用TextBox)控件,该 ...

  4. 如何实现IOS_SearchBar搜索栏及关键字高亮

    搜索框的效果演示: 这个就是所谓的搜索框了,那么接下来我们看看如何使用代码来实现这个功能. 我所使用的数据是英雄联盟的英雄名单,是一个JSON数据的txt文件, JSON数据的处理代码如下所示: // ...

  5. 使用EditText+ListView并结合TextWatcher实现输入关键字筛选数据

    想必大家应该遇到过这样的情况,当点击Spinner控件后弹出的列表内容超多,一个一个滑动着去寻找所要的项很麻烦,尤其是当自己知道想要选择的内容,这时候如果我们只需要输入某些关键字,就可以讲上百条数据筛 ...

  6. wpf CollectionViewSource与ListBox的折叠、分组显示,及输入关键字 Filter的筛选

    在wpf中虽然ObservableCollection<T>作为ListBox的Itemsource,很好,很强大!但是CollectionViewSource与ListBox才是天作之合 ...

  7. 两种方法实现text输入框中“请输入关键字”的提醒

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 基于scrapy框架输入关键字爬取有关贴吧帖子

    基于scrapy框架输入关键字爬取有关贴吧帖子 站点分析 首先进入一个贴吧,要想达到输入关键词爬取爬取指定贴吧,必然需要利用搜索引擎 点进看到有四种搜索方式,分别试一次,观察url变化 我们得知: 搜 ...

  9. Python:输入关键字进行百度搜索并爬取搜索结果

    学习自:手把手教你用Python爬取百度搜索结果并保存 - 云+社区 - 腾讯云 如何利用python模拟百度搜索,Python交流,技术交流区,鱼C论坛 指定关键字,对其进行百度搜索,保存搜索结果, ...

随机推荐

  1. JVM终结篇

    1.1 重新认知JVM 之前我们画过一张图,是从Class文件到类装载器,再到运行时数据区的过程.现在咱们把这张图不妨丰富完善一下,展示了JVM的大体物理结构图. 1.2 GC优化 内存被使用了之后, ...

  2. HDU 3247 Resource Archiver(AC自动机 + 状压DP + bfs预处理)题解

    题意:目标串n( <= 10)个,病毒串m( < 1000)个,问包含所有目标串无病毒串的最小长度 思路:貌似是个简单的状压DP + AC自动机,但是发现dp[1 << n][ ...

  3. SPN在域环境中的应用

    0X01 SPN    服务主体名称(SPN)是Kerberos客户端用于唯一标识给特定Kerberos目标计算机的服务实例名称.Kerberos身份验证使用SPN将服务实例与服务登录帐户相关联.如果 ...

  4. 解决debian (Friendly ARM 嵌入式板)的sudo等一部分命令无法TAB补全

    TAB对于比较长的命令在使用时是十分方便的,最近就遇到TAB 键无法补全sudo后跟的命令的情况因此去网上取经.在一篇博客中找到解决问题的方法,觉得大牛们写的太精炼然后自己做如下总结方便自已以后解决类 ...

  5. sentry can not delete release bug

    sentry can not delete release bug bug $ ./node_modules/@sentry/cli/bin/sentry-cli releases list $ ./ ...

  6. React Hooks: useReducer All In One

    React Hooks: useReducer All In One useReducer https://reactjs.org/docs/hooks-reference.html#usereduc ...

  7. record terminal sessions

    record terminal sessions asciinema https://asciinema.org/ # install $ brew install asciinema # Start ...

  8. full stack & front end

    full stack & front end https://github.com/frank-lam/fullstack-tutorial https://github.com/haizli ...

  9. DoH & DNS over HTTPS

    DoH & DNS over HTTPS DNS over HTTPS(DoH)服务 http://mozilla.com.cn/thread-422231-1-1.html https:// ...

  10. H5 & animation

    H5 & animation https://m.tb.cn/h.VYB7BAx?sm=51fda6 UA checker webp image & css animation CDN ...