• 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;
            }



      

  • 相关阅读:
    span与a元素的键盘聚焦性以及键盘点击性研究——张鑫旭
    小tip: 某简单的字符重叠与图形生成----张鑫旭
    HTML5 number类型文本框step属性的验证机制——张鑫旭
    小tip:FireFox下文本框/域百分比padding bug解决——张鑫旭
    视区相关单位vw, vh..简介以及可实际应用场景——张鑫旭
    好吧,CSS3 3D transform变换,不过如此!——张鑫旭
    HttpWebRequest.Proxy属性
    js动态函数
    js && ||
    eclipse工具栏sdk和avd图标
  • 原文地址:https://www.cnblogs.com/Zingu/p/11769542.html
Copyright © 2020-2023  润新知