将日志信息(提示、警告、错误、历史报警)显示到RichTextBox组件上、点击某个事件按钮,会显示相对应的日志信息。效果图如下:
提示信息代码如下:
/// <summary>
/// 提示事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsb_tip_Click(object sender, EventArgs e)
{
if (tsb_tip.Checked)
{
tsb_warn.Checked = false;
tsb_error.Checked = false;
tsb_historyAlarm.Checked = false;
tbx_msg.Clear();
tbx_msg.ForeColor = Color.FromArgb(48, 48, 48);
//使用for循环遍历Configuration.msgItems集合中的每个元素
for (int i = 0; i< Configuration.msgItems.Count; i++)
{
//对于每个元素,检查其infoType属性是否等于InfoType.Tip
if (Configuration.msgItems[i].infoType == InfoType.Tip)
{
//如果文本框tbx_msg是空的(即tbx_msg.Text == string.Empty)
if (tbx_msg.Text == string.Empty)
{
//直接将时间、空格和消息内容追加到文本框中
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
}
else
{
//如果文本框不为空,则在追加新消息前,先追加一个换行符(Environment.NewLine),然后追加时间、空格和消息内容
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
}
}
}
}
else
{
tbx_msg.Clear();
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
tbx_msg.SelectionStart = tbx_msg.Text.Length;
tbx_msg.SelectionLength = 0;
switch (Configuration.msgItems[i].infoType)
{
case InfoType.Tip:
tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Warn:
tbx_msg.SelectionColor = Color.Orange;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Error:
tbx_msg.SelectionColor = Color.Red;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
}
}
}
tbx_msg.ScrollToCaret();
}
警告事件代码如下:
/// <summary>
/// 警告事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsb_warn_Click(object sender, EventArgs e)
{
if (tsb_warn.Checked)
{
tsb_tip.Checked = false;
tsb_error.Checked = false;
tsb_historyAlarm.Checked = false;
tbx_msg.Clear();
tbx_msg.ForeColor = Color.Orange;
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
if (Configuration.msgItems[i].infoType == InfoType.Warn)
{
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
}
}
}
else
{
tbx_msg.Clear();
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
tbx_msg.SelectionStart = tbx_msg.Text.Length;
tbx_msg.SelectionLength = 0;
switch (Configuration.msgItems[i].infoType)
{
case InfoType.Tip:
tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Warn:
tbx_msg.SelectionColor = Color.Orange;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Error:
tbx_msg.SelectionColor = Color.Red;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
}
}
}
tbx_msg.ScrollToCaret();
}
错误事件代码如下:
/// <summary>
/// 错误事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsb_error_Click(object sender, EventArgs e)
{
if (tsb_error.Checked)
{
tsb_tip.Checked = false;
tsb_warn.Checked = false;
tsb_historyAlarm.Checked = false;
tbx_msg.Clear();
tbx_msg.ForeColor = Color.Red;
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
if (Configuration.msgItems[i].infoType == InfoType.Error)
{
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
}
}
}
else
{
tbx_msg.Clear();
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
tbx_msg.SelectionStart = tbx_msg.Text.Length;
tbx_msg.SelectionLength = 0;
switch (Configuration.msgItems[i].infoType)
{
case InfoType.Tip:
tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Warn:
tbx_msg.SelectionColor = Color.Orange;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Error:
tbx_msg.SelectionColor = Color.Red;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
}
}
}
tbx_msg.ScrollToCaret();
}
历史报警事件代码如下:
/// <summary>
/// 历史报警
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tsb_historyAlarm_Click(object sender, EventArgs e)
{
if (tsb_historyAlarm.Checked)
{
tsb_tip.Checked = false;
tsb_warn.Checked = false;
tsb_error.Checked = false;
tbx_msg.Clear();
tbx_msg.ForeColor = Color.Red;
foreach (KeyValuePair<DateTime, string> item in Configuration.Instance.historyAlarm)
{
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(item.Key.ToString("yyyy_MM_dd HH:mm:ss") + " " + item.Value);
else
tbx_msg.AppendText(Environment.NewLine + item.Key.ToString("yyyy_MM_dd HH:mm:ss") + " " + item.Value);
}
}
else
{
tbx_msg.Clear();
for (int i = 0; i < Configuration.msgItems.Count; i++)
{
tbx_msg.SelectionStart = tbx_msg.Text.Length;
tbx_msg.SelectionLength = 0;
switch (Configuration.msgItems[i].infoType)
{
case InfoType.Tip:
tbx_msg.SelectionColor = Color.FromArgb(48, 48, 48);
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Warn:
tbx_msg.SelectionColor = Color.Orange;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
case InfoType.Error:
tbx_msg.SelectionColor = Color.Red;
if (tbx_msg.Text == string.Empty)
tbx_msg.AppendText(Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
else
tbx_msg.AppendText(Environment.NewLine + Configuration.msgItems[i].time + " " + Configuration.msgItems[i].msg);
break;
}
}
}
tbx_msg.ScrollToCaret();
}
可根据自己需求自行修改补充代码。