首页 > 代码库 > 一个简易的C#信息展示板

一个简易的C#信息展示板

本文中的展示板是用TextBox实现的,因为工作需要,我实现了一个向TextBox自动生成格式化信息的工具类DisplayBoardHelper

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DisplayBoard
{
    public class DisplayBoardHelper
    {
        /// <summary>
        /// 事件等级
        /// </summary>
        public enum LogLevel
        {
            /// <summary>
            /// 调试
            /// </summary>
            DEBUG = 0,
            /// <summary>
            /// 信息
            /// </summary>
            INFO = 1,
            /// <summary>
            /// 通知
            /// </summary>
            NOTICE = 2,
            /// <summary>
            /// 警告
            /// </summary>
            WARNING = 3,
            /// <summary>
            /// 错误
            /// </summary>
            ERROR = 4,
            /// <summary>
            /// 严重
            /// </summary>
            CRIT = 5,
            /// <summary>
            /// 危险
            /// </summary>
            ALERT = 6,
            /// <summary>
            /// 紧急
            /// </summary>
            EMERG = 7
        }

        /// <summary>
        /// 将报警等级的枚举打印成字符串
        /// </summary>
        /// <param name="level">事件等级</param>
        /// <param name="lang">输出字符串语言 CHS:简体中文 ENG:英语</param>
        /// <returns></returns>
        private static string LevelToString(LogLevel level, string lang = "CHS")
        {
            if (lang == "CHS")
            {
                switch (level)
                {
                    case LogLevel.DEBUG: return "调试";
                    case LogLevel.INFO: return "信息";
                    case LogLevel.NOTICE: return "通知";
                    case LogLevel.WARNING: return "警告";
                    case LogLevel.ERROR: return "错误";
                    case LogLevel.CRIT: return "严重";
                    case LogLevel.ALERT: return "危险";
                    case LogLevel.EMERG: return "紧急";
                    default: return "未知";
                }
            }
            else if (lang == "ENG")
            {
                switch (level)
                {
                    case LogLevel.DEBUG: return "DEBUG";
                    case LogLevel.INFO: return "INFO";
                    case LogLevel.NOTICE: return "NOTICE";
                    case LogLevel.WARNING: return "WARNING";
                    case LogLevel.ERROR: return "ERROR";
                    case LogLevel.CRIT: return "CRIT";
                    case LogLevel.ALERT: return "ALERT";
                    case LogLevel.EMERG: return "EMERG";
                    default: return "UNKNOWN";
                }
            }
            else
            {
                throw new Exception(lang + ":未知语言类型");
            }
        }

        /// <summary>
        /// 向指定的TextBox中添加一条新日志
        /// </summary>
        /// <param name="tb">TextBox对象</param>
        /// <param name="logLevel">日志等级</param>
        /// <param name="logInfo">日志信息</param>
        /// <param name="lang">使用语言</param>
        public static void AddLog(System.Windows.Forms.TextBox tb, 
            LogLevel logLevel, string logInfo, string lang = "CHS")
        {
            string sNewLog = string.Format("[{0}][{1}]:{2}\r\n",
                DateTime.Now.ToLongTimeString(),
                LevelToString(logLevel, lang),
                logInfo);

            tb.Text += sNewLog;
            tb.Select(tb.TextLength, 0);
            tb.ScrollToCaret();
        }

        /// <summary>
        /// 查看TextBox的设置是否符合库最佳使用环境(这个函数并不是必须的,可以选择性调用)
        /// </summary>
        /// <param name="tb">TextBox对象</param>
        /// <param name="suggestion">建议(返回false时可查看)</param>
        /// <returns>true:合理 false:不合理</returns>
        public static bool IsConfigReasonable(
            System.Windows.Forms.TextBox tb,
            out string suggestion)
        {
            bool isConfigReasonable = true;
            suggestion = "建议:\n";

            //TextBox应被设置为支持多行
            if (tb.Multiline == false)
            {
                suggestion += "控件" + tb.Name + "只支持单行文本(Multiline)\n";
                isConfigReasonable = false;
            }

            //TextBox应被设置为支持纵向滚动条
            if (tb.ScrollBars != System.Windows.Forms.ScrollBars.Vertical ||
                tb.ScrollBars != System.Windows.Forms.ScrollBars.Both)
            {
                suggestion += "控件" + tb.Name + "缺少纵向滚动条(ScrollBars)\n";
                isConfigReasonable = false;
            }

            //ReadOnly属性和Enable属性应至少设置一个确保TextBox只读
            if (tb.ReadOnly == false && tb.Enabled == true)
            {
                suggestion += "控件" + tb.Name + "可任意修改(ReadOnly or Enabled)\n";
                isConfigReasonable = false;
            }

            //没有问题,不需要建议
            if (isConfigReasonable)
            {
                suggestion = "";
            }

            return isConfigReasonable;
        }
    }
}

下面是这个示例程序的实现,这个示例程序中只有一个Dock属性被设置为Fill的TextBox

程序代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DisplayBoard
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            //写入一些日志
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.DEBUG, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.INFO, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.NOTICE, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.WARNING, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.ERROR, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.CRIT, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.ALERT, "这是一条测试信息");
            DisplayBoardHelper.AddLog(this.txtDisplayBoard, 
                DisplayBoardHelper.LogLevel.EMERG, "这是一条测试信息");

            //查看TextBox的设置是否符合库最佳使用环境
            string suggestion;
            bool b = DisplayBoardHelper.IsConfigReasonable(
                this.txtDisplayBoard, out suggestion);
            if (!b)
            {
                MessageBox.Show(suggestion);
            }
        }
    }
}

运行结果示例

DisplayBoardHelper.IsConfigReasonable生成的使用环境建议


END


一个简易的C#信息展示板