您的位置:首页 > 编程语言 > C#

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

2017-08-16 17:57 323 查看
本文转载连接: 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;

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: