C#自定义控件—文本显示、文本设值

C#用户控件之文本显示、设定组件

如何绘制一个便捷的文本显示组件、文本设值组件(TextShow,TextSet)?

绘制此控件的目的就是方便一键搞定标签显示(可自定义方法显示文本颜色等),方便自定义方法又省略了挨个拖拽的过程

纯定义属性

【文本设定】:字体、标签、值、单位;事件方法:Enter、Leave、KeyDown

【文本显示】:变量名称、变量值、单位、字体、控件刻度

直接上代码


【文本设定】

复制代码
public partial class TextSet : UserControl
{
    public TextSet()
    {
        InitializeComponent();
        this.txt_Value.ReadOnly = true;
    }

    #region 属性  字体、标签、值、单位

    private Font textFont = new Font("微软雅黑", 12);
    [Browsable(true)]
    [Category("布局_G")]
    [Description("字体格式")]
    public Font TextFont
    {
        get { return textFont; }
        set
        {
            if (value != null)
            {
                textFont = value;
                this.lbl_Title.Font = this.lbl_Unit.Font = this.txt_Value.Font = textFont;
            }
        }
    }

    private Color textColor = Color.Black;
    [Browsable(true)]
    [Category("布局_G")]
    [Description("文本颜色")]
    public Color TextColor
    {
        get { return textColor; }
        set
        {
            textColor = value;
            this.lbl_Title.ForeColor = this.lbl_Unit.ForeColor = this.txt_Value.ForeColor = textColor;
        }
    }

    private float textScale = 0.37f;
    [Browsable(true)]
    [Category("布局_G")]
    [Description("控件刻度")]
    public float TextScale
    {
        get { return textScale; }
        set
        {
            textScale = value;
            this.tableLayoutPanel1.ColumnStyles[0].Width = (this.Width - textScale * this.Width) * 0.75f;
            this.tableLayoutPanel1.ColumnStyles[1].Width = textScale * this.Width;
            this.tableLayoutPanel1.ColumnStyles[2].Width = (this.Width - textScale * this.Width) * 0.25f;
        }
    }

    private string varTitle = "变量名称";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("变量名称")]
    public string VarTitle
    {
        get { return varTitle; }
        set
        {
            varTitle = value;
            this.lbl_Title.Text = varTitle;
        }
    }

    private string varValue = "21.50";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("输入值")]
    public string VarValue
    {
        get { return varValue; }
        set
        {
            varValue = value;
            this.txt_Value.Text = varValue;
        }
    }

    private string varUnit = "℃";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("单位")]
    public string VarUnit
    {
        get { return varUnit; }
        set
        {
            varUnit = value;
            this.lbl_Unit.Text = varUnit;
        }
    }

    #endregion

    #region  输入使能事件

    //正在输入标志位
    public bool IsSetting { get; set; }

    private void txt_Value_Enter(object sender, EventArgs e)
    {
        IsSetting = true;
        this.txt_Value.ReadOnly = false;
    }

    private void txt_Value_Leave(object sender, EventArgs e)
    {
        IsSetting = false;
        this.txt_Value.ReadOnly = true;
    }

    //添加输入完成事件
    public event EventHandler SettingChanged;

    private void txt_Value_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            //技巧:输入完成移动焦点~输入框变灰
            this.lbl_Title.Focus();

            //激活触发事件
            SettingChanged?.Invoke(this, e);
        }
    }
    #endregion
}

【文本显示】

复制代码
public partial class TextShow : UserControl
{
    public TextShow()
    {
        InitializeComponent();
    }

    #region Fields 变量名称、变量值、单位、字体、控件刻度
    //[Browsable(true)]
    //[Category("布局_G")]
    //[Description("变量名称")]
    //public String VarName { get; set; }


    private Font textFont = new Font("Segoe UI Variable Display", 15, FontStyle.Bold);
    [Browsable(true)]
    [Category("布局_G")]
    [Description("字体格式")]
    public Font TextFont
    {
        get { return textFont; }
        set
        {
            if (value != null)
            {
                textFont = value;
                this.lbl_Value.Font = this.lbl_Unit.Font = textFont;
            }
        }
    }

    private Color textColor = Color.Blue;
    [Browsable(true)]
    [Category("布局_G")]
    [Description("文本颜色")]
    public Color TextColor
    {
        get { return textColor; }
        set
        {
            textColor = value;
            this.lbl_Value.ForeColor = this.lbl_Unit.ForeColor = textColor;
        }
    }

    private string varVlaue = "1.0E-5";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("变量值")]
    public string VarVlaue
    {
        get { return varVlaue; }
        set
        {
            varVlaue = value;
            this.lbl_Value.Text = varVlaue;
        }
    }

    private string unit = "Pa";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("单位")]
    public string Unit
    {
        get { return unit; }
        set
        {
            unit = value;
            this.lbl_Unit.Text = unit;
        }
    }

    private float textScale = 0.6f;
    [Browsable(true)]
    [Category("布局_G")]
    [Description("控件刻度")]
    public float TextScale
    {
        get { return textScale; }
        set
        {
            textScale = value;
            this.tableLayoutPanel1.ColumnStyles[0].Width = textScale * this.Width;
            this.tableLayoutPanel1.ColumnStyles[1].Width = this.Width - textScale * this.Width;
        }
    }

    #endregion

自定义绘制组件更方便以后直接使用,是一件一劳永逸的事情。

End

相关推荐
凌镜icecraft1 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
Jeff-Tsai1 天前
一个不用注册方法的 JSON-RPC 包,怎么决定哪些方法能被调用?
c#·.net·asp.net core·json-rpc
rockey6272 天前
AScript正式推出Template模板引擎
c#
沐言Agent2 天前
做工业上位机,这个 C# 开源框架把 PLC 通讯、流程控制和 MES 对接都备好了
c#·工业上位机
投研帮2 天前
C#文档管理系统开发实战:技术选型、全文检索与并发控制
c#·.net·文档管理系统
2395730493 天前
在 Avalonia 12 上用纯 .NET 重写 Monaco 编辑器:TKCode.Arc CodeEditor / DiffEditor / LSP 深度架构与实战
c#·.net·lsp·avalonia·monaco·codeeditor·tkcode.arc
新*3 天前
在 .NET 里嵌入 IronPython:内存只涨不降的真相与正确的封装姿势
c#·.net·内存优化·ironpython·dlr
2501_930707784 天前
使用C#从 PowerPoint 演示文稿中提取图片
开发语言·c#·powerpoint
zhanghaha13144 天前
C# 系列教程_15 类 Class 保姆级系统教程(面向新手,大量示例 + 底层机理)
java·开发语言·c#
Risehuxyc4 天前
HttpClient怎么传POST参数
c#