• C# winForm ListBox 每行显示不同颜色设置


    使用ListBox时,每行显示不同的颜色

    1、把AllowHtmlDraw属性设置为True

    2、直接在Add或者AddRange里写标签

    代码里用的是DEV插件里的ListBox,使用原生的也是一样的写法

    使用AddRange写法:

    1 listBoxControl2.AllowHtmlDraw = DevExpress.Utils.DefaultBoolean.True;
    2 
    3 listBoxControl2.Items.AddRange(new object[] {
    4                         "我是红色的 <color=Red>Red</color>",
    5                         "<color=Green>我是绿色的</color>",
    6                         "<color=Blue>Blue</color> 我是蓝色的"
    7                     });

    使用Add写法:

    listBoxControl.Items.Add("<color=Red>我是红色的</color>");

    可以封装一下:

    private void ListMsg(string msg, MsgLevel msgLevel)
            {
                listBoxControl.BeginInvoke(new Action(() =>
                {
                    listBoxControl.Items.Add("<color=" + GetColor(msgLevel) + ">" + DateTime.Now.ToString("HH:mm") + " " + msg + "</color>");
    
                    //滚动到最后一行
                    if (this.listBoxControl.Items.Count > 0)
                    {
                        this.listBoxControl.SelectedIndex = listBoxControl.Items.Count - 1;
                    }
                }));
            }

    枚举:

    private enum MsgLevel
            {
                /// <summary>
                /// 0.调试信息输出
                /// </summary>
                Debug = 0,
                /// <summary>
                /// 1.业务信息记录
                /// </summary>
                Info = 1,
                /// <summary>
                /// 2.警告提醒(捕获的业务异常)
                /// </summary>
                Warn = 2,
                /// <summary>
                /// 3.发生了异常(捕获的系统异常)
                /// </summary>
                Exception = 3,
                /// <summary>
                /// 4.发生致命异常(未被捕获的异常|捕获的业务逻辑异常)
                /// </summary>
                Fatal = 4
            }
            private string GetColor(MsgLevel msgLevel)
            {
                string strColor = "Black";
                switch (msgLevel)
                {
                    case MsgLevel.Debug:
                        strColor = "Black";
                        break;
                    case MsgLevel.Info:
                        strColor = "Black";
                        break;
                    case MsgLevel.Warn:
                        strColor = "Yellow";
                        break;
                    case MsgLevel.Exception:
                        strColor = "Purple";
                        break;
                    case MsgLevel.Fatal:
                        strColor = "Red";
                        break;
                    default:
                        strColor = "Black";
                        break;
                }
                return strColor;
            }

    随着时光的流逝,或许所有存在的东西都会消失,若最初,我们是奔着一个方向而往,最终,却很有可能会寻觅到了另外一种存在,也并非是忘记初心,而是时光早已赋予它不同的使命。

    现实有多残酷,我们就有多坚强。
    别说生活欺骗了你世界的模样,取决于你凝聚他的目光。
    从怯懦到勇敢,是给自己最好的礼物。

    所谓的成长,就是有一天我们可以把最重要的精力,放在最重要的人和最重要的事上。

  • 相关阅读:
    Linux下 高性能、易用、免费的ASP.NET服务器
    K-means算法
    K-means算法
    机器学习之深度学习
    机器学习之深度学习
    Tracking-Learning-Detection (TLD算法总结)
    Tracking-Learning-Detection (TLD算法总结)
    机器学习技法之Aggregation方法总结:Blending、Learning(Bagging、AdaBoost、Decision Tree)及其aggregation of aggregation
    机器学习技法之Aggregation方法总结:Blending、Learning(Bagging、AdaBoost、Decision Tree)及其aggregation of aggregation
    迭代是人,递归是神(迭代与递归的总结:比较)
  • 原文地址:https://www.cnblogs.com/boyxzhen/p/8443799.html
Copyright © 2020-2023  润新知