【问题标题】:Parsing a Log (text file) with colors in C# Winforms在 C# Winforms 中使用颜色解析日志(文本文件)
【发布时间】:2016-01-31 06:24:44
【问题描述】:

我正在创建一个对色盲友好的日志阅读器,它将解析默认颜色为蓝色的日志文件(文本文件)。当一行包含“错误:”时,它应该将颜色设置为橙色。听起来很简单,但我编写的代码似乎没有检测到该行中包含“ERROR:”。这是我的代码:

using System;
using System.IO;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WWIV5TelnetServer
{
    public partial class LogForm : Form
    {
        public LogForm()
        {
            InitializeComponent();
        }

        private void LogForm_Load(object sender, EventArgs e)
        {
            // Default Number Of Lines Per Log
            int logLength  = Int32.Parse(logLines.Text);

            // Network Log
            string errorText1 = " ERROR: ";
            var i1 = 0;
            // From Bottom To Top
            var lines1 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\network.log").Reverse().Skip(1);

            foreach (string line1 in lines1)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line1.Contains(errorText1))
                {
                    listBox1.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox1.Items.Add(line1);
                }
                else
                {
                    listBox1.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox1.Items.Add(line1);
                }
                i1++;
                if (i1 >= logLength) break;
            }

            // Networkb Log
            string errorText2 = " ERROR: ";
            var i2 = 0;
            // From Bottom To Top
            var lines2 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\networkb.log").Reverse().Skip(1);

            foreach (string line2 in lines2)
            {
                // Catch ERROR: And Color Orange Else Blue
                if (line2.Contains(errorText2))
                {
                    listBox2.ForeColor = Color.FromArgb(230, 159, 0);
                    this.listBox2.Items.Add(line2);
                }
                else
                {
                    listBox2.ForeColor = Color.FromArgb(0, 114, 178);
                    this.listBox2.Items.Add(line2);
                }
                i2++;
                if (i2 >= logLength) break;
            }

            // Net.log
            string errorText3 = " ERROR: ";
            var i3 = 0;
            // From Bottom To Top
            var lines3 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\gfiles\NET.LOG");

            foreach (string line3 in lines3)
            {
                // Default Color Blue
                listBox3.ForeColor = Color.FromArgb(0, 114, 178);

                // Catch ERROR: And Color Orange Else Blue
                if (line3.Contains(errorText3))
                {
                    listBox3.ForeColor = Color.FromArgb(230, 159, 0);
                }
                this.listBox3.Items.Add(line3);
                i3++;
                if (i3 >= logLength) break;
            }

            // Change Log
            var lines4 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\changelog.txt");

            foreach (string line4 in lines4)
            {
                // Default Color Black
                listBox4.ForeColor = Color.Black;

                this.listBox4.Items.Add(line4);
            }

            // What's New
            var lines5 = File.ReadAllLines(Properties.Settings.Default.homeDirectory + @"\whatsnew.txt");

            foreach (string line5 in lines5)
            {
                // Default Color Black
                listBox5.ForeColor = Color.Black;

                this.listBox5.Items.Add(line5);
            }
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Refresh();
        }
    }
}

我昨天尝试在这方面寻求帮助,并且已经接近答案,但为时已晚,问题已过时。

提前感谢您的帮助。

【问题讨论】:

  • 虽然您可以在所有者绘制模式下绘制不同颜色的组合框项目,但在详细信息视图中使用列表视图更简单,您可以为每个项目设置前景色。

标签: c# winforms visual-studio-2015


【解决方案1】:

问题是您每次插入一行时都会不断更改控件的前景色。因此,如果您插入错误行,所有项目最终都会呈现为橙色。然后,如果您在列表中插入另一个不是错误的项目,您会告诉它以蓝色呈现所有项目。

执行此操作的正确方法是使用 DrawMode 并将其设置为 DrawMode.OwnerFixed。 MSDN 中的一个很好的例子:DrawMode Enumeration

您必须根据 ListBox1.Items[e.Index].ToString() 进行一些颜色更改,而不是基于 e.Index 进行一些颜色更改,其中包含单词“错误:”等。

【讨论】:

    猜你喜欢
    • 2016-05-07
    • 2017-01-30
    • 2012-03-22
    • 1970-01-01
    • 2015-04-26
    • 1970-01-01
    • 2012-10-17
    • 2011-01-17
    • 1970-01-01
    相关资源
    最近更新 更多