Exemplo n.º 1
0
        /// <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 = RichTextBoxHelper.getIndexArray(richTextBox1.Text.ToLower(), str[i]);
                b   += list.Count;
            }
            for (int i = 0; i < str.Length; i++)
            {
                list = RichTextBoxHelper.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);
        }