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. javascript 克隆对象/数组的方法 clone()

      1 11 javascript 克隆对象/数组的方法 clone() 1 demo: code: 1 var Obj; 2 let clone = (Obj) => { 3 var buf; ...

  2. prototype chain & prototype & __proto__

    prototype chain & prototype & proto prototype chain MDN https://developer.mozilla.org/en-US/ ...

  3. ThoughtWorks Homework

    ThoughtWorks Homework Homework 考察知识点 项目搭建 技术选型 测试 编码风格 代码质量 设计模式 数据结构 算法 架构 开源协作 CI/CD DevOps Linux ...

  4. React & Dva & Actions & dispatch & effects

    React & Dva & Actions & dispatch & effects dispatch https://dvajs.com/guide/introduc ...

  5. shit 钉钉

    shit 钉钉 钉钉 圈子 入口, 没有 https://www.dingtalk.com/qidian/help-detail-1000131196.html shit bug 全员圈 这个好像是要 ...

  6. 深入浅出的JS执行机制(图文教程)

    前序 作为一个有理想有抱负的前端攻城狮,想要走向人生巅峰,我们必须将我们使用的功法练到天人合一的地步.我在们日常工作中,使用最多的语言就是JavaScript了,为了写出完美的.能装逼的代码,我们必须 ...

  7. 【Notes_2】现代图形学入门——向量与线性代数

    向量与线性代数 点乘和叉乘 Dot Multiplication 点乘在图形学的应用 (1) 求两个向量之间的夹角: $$\cos(\theta) = \frac{(\vec{a} \cdot \ve ...

  8. 前端axios传递一个包含数组的对象到后台,后台可以用String接收,也可以用List集合接收

    前端代码: data() { return { listQuery: { date: [], } }}, //查询列表信息getList() { if (this.listQuery.date == ...

  9. 调度场算法&&中缀表达式=>后缀表达式

    #include<stdio.h> #include<string.h> int main(void){ char ch,stro[1001],stack[1001]; int ...

  10. 看完我的笔记不懂也会懂----bootstrap

    目录 Bootstrap笔记 知识点扫盲 容器 栅格系统 源码分析部分 外部容器 栅格系统(盒模型)设计的精妙之处 Bootstrap笔记 写在开始: 由于我对AngulaJS的学习只是一个最浅显的过 ...