C#自定義控制元件—文字顯示、文字設值

郭恩硕發表於2024-09-03

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

相關文章