Reputation: 21
I'm creating a log reader that will be Color Blind Friendly that will parse a log file (text file) with the default color of blue. When a line contains "ERROR:" it should set the color to Orange. Sounds simple enough but the code I whipped up does not seem to detect that the line contains "ERROR:" in it. Here is my code:
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();
}
}
}
I tried getting help with this yesterday and was getting close to an answer but it became late and the Question went stale.
Thanks for any help in advance.
Upvotes: 0
Views: 263
Reputation: 2940
The problem is that you keep on changing the foreground color of the control everytime that you insert a line. So if you insert an error line, all items will wind up being rendered in orange. Then, if you insert another item in the list that is not an error you tell it to render all items in, say, blue.
The proper way to do this is to use the DrawMode and set it to DrawMode.OwnerFixed. A good example of this availave in MSDN: DrawMode Enumeration
Instead of doing a switch based on e.Index
you'll have to do some color change based on ListBox1.Items[e.Index].ToString()
containing the word 'error : ' etc.
Upvotes: 2