标签:index 高亮 int list richTextBox1 关键字 Length str RichText
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; }
标签:index,高亮,int,list,richTextBox1,关键字,Length,str,RichText 来源: https://www.cnblogs.com/Zingu/p/11769542.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。