显示字符串颜色 [英] display string color

查看:70
本文介绍了显示字符串颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有2个文本文件并进行比较.如果file1中的行与文件2中的行不同,则它将在文本框中将这些行写为红色.

当我编写下面的代码时,它以红色显示所有文本.我该如何更改?

Hi,

I have 2 text files and compare them. If there is a line in file1 is different than a line in file 2, it will write those line in textboxes with red.

When i wrote the code below, it show all text in red. How can i change them?

string MOFile = "MOOutputTextFile.txt";
           
            string line1, line2;
            var file1 = File.OpenText(WSFile);
            var file2 = File.OpenText(MOFile);

            int counter = 0;
            try
            {
                if((File.Exists(WSFile)) && (File.Exists(MOFile)))
                {
                    while (((line1 = file1.ReadLine()) != null) && (line2 = file2.ReadLine()) != null)
                    {
                        textBox1.Text += line1 + "\r\n";
                        textBox2.Text += line2 + "\r\n";
                        if (!line1.Equals(line2))
                        {
                            textBox1.Text += line1 + "\r\n";
                            textBox2.Text += line2 + "\r\n";
                            textBox1.ForeColor = Color.Red;
                           
                        }
                        counter++;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }

推荐答案

您可以保守秘密吗?然后听:字符串没有颜色.

您是否注意到textBox1.ForeColor = Color.Red;?这没有任何意义.您只需一次又一次地以相同的颜色不断绘制相同的控件,并具有非凡的毅力,值得更好的应用.

怎么改变呢?通过停下来做.您只需分配属性ForColor一次.还有更多颜色,您知道吗?

-SA
Can you keep a secret? Then listen: strings don''t have color.

Did you noticed textBox1.ForeColor = Color.Red;? It does not make any sense. You just keep painting the same control with the same color in cycle, again and again, with remarkable perseverance which deserves better application.

How to change it? By stopping to do it. You need to assign the property ForColor only once. And there are more colors, did you know?

—SA


从此处开始:
Start here: Multiple Colored Texts in RichTextBox using C#[^].

A textbox is the wrong control for the behavior you have described.


这篇关于显示字符串颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆