当前位置: 首页 > article >正文

C#控件开发3—文本显示、文本设值

目录

  • 1.文本设置
    • 1)定义属性
    • 2)定义事件
  • 2.本文显示
    • 1) 定义属性
    • 2)定义事件
  • End

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

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

1.文本设置

1)定义属性

  • 属性:字体、标签、值、单位
 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

2)定义事件

  • 键入使能方法、事件
#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

2.本文显示

1) 定义属性

  • 变量名称、变量值、单位、字体、控件刻度
//指定默认事件(双击控件进入)
[DefaultEvent("TextShowClick")]
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 varValue = "1.0E-5";
    [Browsable(true)]
    [Category("布局_G")]
    [Description("变量值")]
    public string VarValue
    {
        get { return varValue; }
        set
        {
            varValue = value;
            this.lbl_Value.Text = varValue;
        }
    }

    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

2)定义事件

  • 鼠标双击控件后进入自定义事件
   #region Event 鼠标双击控件后进入自定义事件
   //创建委托—事件
   public delegate void BtnClickDelegate(object sender, EventArgs e);
   [Browsable(true)]
   [Category("操作_G")]
   [Description("文本双击触发事件")]
   public event BtnClickDelegate TextShowClick;

   private void Lbl_Value_DoubleClick(object sender, EventArgs e)
   {
       TextShowClick?.Invoke(this, new EventArgs());
   }

   #endregion

End


http://www.kler.cn/a/454900.html

相关文章:

  • Ubuntu 22.04.5 修改IP
  • 面试突击-JAVA集合类(持续更新...)
  • 【SQL】筛选某一列字段中,截取含有关键词“XX”字段位置的前4个字段,去重后查看字段
  • PDF书籍《手写调用链监控APM系统-Java版》第11章 插件与链路的结合:HttpClient插件实现跨进程传输TraceSegment
  • OpenCV-Python实战(8)——图像变换
  • 探索基金聚合平台的背景与发展:Finanzen.net、Franklin Templeton、Finect
  • STM32完全学习——FATFS0.15移植SD卡
  • AI客服机器人如何帮助企业应对高峰期客户咨询压力?
  • LeetCode--239.滑动窗口最大值(使用双端队列解决)
  • docker拉取rabbitmq镜像时报Client.Timeout exceeded while awaiting header错误处理办法
  • Linux-Ubuntu之串口通信
  • ChatGPT助力数据可视化与数据分析效率的提升(二)
  • lua debug相关方法详解
  • leetcode82:删除链表中的重复元素II
  • 【蓝桥杯】走迷宫
  • 题海拾贝:蓝桥杯 2020 省AB 乘法表
  • 免费资源网站
  • ANSYS EMC Plus:谐振腔中的天线
  • Markdown语法字体字号讲解
  • git revert