现在的位置: 首页 > 综合 > 正文

richtextbox的使用

2013年03月28日 ⁄ 综合 ⁄ 共 3240字 ⁄ 字号 评论关闭

        private void initTxtDiff()
        {
            DirectoryInfo di = new DirectoryInfo(curStdPath);
            string stdcase="";
            foreach (FileInfo file in di.GetFiles())
            {
                if (file.Extension == ".uof")
                {
                    stdcase=file.FullName;
                    break;
                }
            }

            di = new DirectoryInfo(curUofPath);
            string uofFile = "";
            foreach (FileInfo file in di.GetFiles())
            {
                if (file.Extension == ".uof")
                {
                    uofFile = file.FullName;
                    break;
                }
            }

            List<int> color1 = new List<int>();
            List<int> color2 = new List<int>();
            List<String> textlist1 = new List<String>();
            List<String> textlist2 = new List<String>();
            StreamReader srStdCase = new StreamReader(stdcase, System.Text.Encoding.GetEncoding("UTF-8"));
            string line;
            while ((line = srStdCase.ReadLine()) != null)
            {
                textlist1.Add(line);
            }
            StreamReader srTest = new StreamReader(uofFile, System.Text.Encoding.GetEncoding("UTF-8"));
            while ((line = srTest.ReadLine()) != null)
            {
                textlist2.Add(line);
            }
            srStdCase.Close();
            srTest.Close();

            int length = textlist1.Count <= textlist2.Count ? textlist1.Count : textlist2.Count;
            for (int i = 0; i < length; i++)
            {
                int exist1 = richTextBox1.Text.Length;
                int exist2 = richTextBox2.Text.Length;
                richTextBox1.Text += textlist1[i] + "/n";
                richTextBox2.Text += textlist2[i] + "/n";//最后一行需要剪裁
                if (!textlist1[i].Equals(textlist2[i]))
                {
                    color1.Add(exist1);
                    color1.Add(textlist1[i].Length);
                    color2.Add(exist2);
                    color2.Add(textlist2[i].Length);
                }    
            }

            if (textlist1.Count > length)
            {
                int exist = richTextBox1.Text.Length;
                for (int i = length; i < textlist1.Count; i++)
                {
                    richTextBox1.Text += textlist1[i] + "/n";
                }
                int alen = richTextBox1.Text.Length - 1;//最后一行的换行符不算
                color1.Add(exist);
                color1.Add(alen - exist);
            }
            else
            {
                int exist = richTextBox2.Text.Length;
                for (int i = length; i < textlist2.Count; i++)
                {
                    richTextBox2.Text += textlist2[i] + "/n";
                }
                int alen = richTextBox2.Text.Length - 1;//最后一行的换行符不算
                color2.Add(exist);
                color2.Add(alen - exist);
            }
            richTextBox1.Text = richTextBox1.Text.Substring(0, richTextBox1.Text.Length - 1);
            richTextBox2.Text = richTextBox2.Text.Substring(0, richTextBox2.Text.Length - 1);

            //最后着色,因为当文本改变时,高亮会消失。
            for (int i = 0; i < color1.Count; i += 2)
            {
                richTextBox1.Select(color1[i], color1[i + 1]);
                richTextBox1.SelectionBackColor = Color.Red;
            }
            for (int i = 0; i < color2.Count; i += 2)
            {
                richTextBox2.Select(color2[i], color2[i + 1]);
                richTextBox2.SelectionBackColor = Color.Red;
            }
        }

抱歉!评论已关闭.