c# RichTextBox用法——设置指定字符串的颜色

来源:互联网 发布:金鼎娱乐源码下载 编辑:程序博客网 时间:2024/06/08 19:51

本文转载连接: http://blog.csdn.net/crazytaliban/article/details/52002657


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Collections;


namespace RichTextBoxUse

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }


        private void button1_Click(object sender, EventArgs e)

        {

            ArrayList list = getIndexArray(richTextBox1.Text, "str1");

            for (int i = 0; i < list.Count; i++)

            {

                int index = (int)list[i];

                richTextBox1.Select(index, "str1".Length);

                richTextBox1.SelectionColor = Color.Red;

            }

        }


        private 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;

        }

    }

}