翼度科技»论坛 编程开发 .net 查看内容

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

4

主题

4

帖子

12

积分

新手上路

Rank: 1

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

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

纯定义属性
【文本设定】:字体、标签、值、单位;事件方法:Enter、Leave、KeyDown
【文本显示】:变量名称、变量值、单位、字体、控件刻度
直接上代码
【文本设定】
  1. public partial class TextSet : UserControl
  2. {
  3.     public TextSet()
  4.     {
  5.         InitializeComponent();
  6.         this.txt_Value.ReadOnly = true;
  7.     }
  8.     #region 属性  字体、标签、值、单位
  9.     private Font textFont = new Font("微软雅黑", 12);
  10.     [Browsable(true)]
  11.     [Category("布局_G")]
  12.     [Description("字体格式")]
  13.     public Font TextFont
  14.     {
  15.         get { return textFont; }
  16.         set
  17.         {
  18.             if (value != null)
  19.             {
  20.                 textFont = value;
  21.                 this.lbl_Title.Font = this.lbl_Unit.Font = this.txt_Value.Font = textFont;
  22.             }
  23.         }
  24.     }
  25.     private Color textColor = Color.Black;
  26.     [Browsable(true)]
  27.     [Category("布局_G")]
  28.     [Description("文本颜色")]
  29.     public Color TextColor
  30.     {
  31.         get { return textColor; }
  32.         set
  33.         {
  34.             textColor = value;
  35.             this.lbl_Title.ForeColor = this.lbl_Unit.ForeColor = this.txt_Value.ForeColor = textColor;
  36.         }
  37.     }
  38.     private float textScale = 0.37f;
  39.     [Browsable(true)]
  40.     [Category("布局_G")]
  41.     [Description("控件刻度")]
  42.     public float TextScale
  43.     {
  44.         get { return textScale; }
  45.         set
  46.         {
  47.             textScale = value;
  48.             this.tableLayoutPanel1.ColumnStyles[0].Width = (this.Width - textScale * this.Width) * 0.75f;
  49.             this.tableLayoutPanel1.ColumnStyles[1].Width = textScale * this.Width;
  50.             this.tableLayoutPanel1.ColumnStyles[2].Width = (this.Width - textScale * this.Width) * 0.25f;
  51.         }
  52.     }
  53.     private string varTitle = "变量名称";
  54.     [Browsable(true)]
  55.     [Category("布局_G")]
  56.     [Description("变量名称")]
  57.     public string VarTitle
  58.     {
  59.         get { return varTitle; }
  60.         set
  61.         {
  62.             varTitle = value;
  63.             this.lbl_Title.Text = varTitle;
  64.         }
  65.     }
  66.     private string varValue = "21.50";
  67.     [Browsable(true)]
  68.     [Category("布局_G")]
  69.     [Description("输入值")]
  70.     public string VarValue
  71.     {
  72.         get { return varValue; }
  73.         set
  74.         {
  75.             varValue = value;
  76.             this.txt_Value.Text = varValue;
  77.         }
  78.     }
  79.     private string varUnit = "℃";
  80.     [Browsable(true)]
  81.     [Category("布局_G")]
  82.     [Description("单位")]
  83.     public string VarUnit
  84.     {
  85.         get { return varUnit; }
  86.         set
  87.         {
  88.             varUnit = value;
  89.             this.lbl_Unit.Text = varUnit;
  90.         }
  91.     }
  92.     #endregion
  93.     #region  输入使能事件
  94.     //正在输入标志位
  95.     public bool IsSetting { get; set; }
  96.     private void txt_Value_Enter(object sender, EventArgs e)
  97.     {
  98.         IsSetting = true;
  99.         this.txt_Value.ReadOnly = false;
  100.     }
  101.     private void txt_Value_Leave(object sender, EventArgs e)
  102.     {
  103.         IsSetting = false;
  104.         this.txt_Value.ReadOnly = true;
  105.     }
  106.     //添加输入完成事件
  107.     public event EventHandler SettingChanged;
  108.     private void txt_Value_KeyDown(object sender, KeyEventArgs e)
  109.     {
  110.         if (e.KeyCode == Keys.Enter)
  111.         {
  112.             //技巧:输入完成移动焦点~输入框变灰
  113.             this.lbl_Title.Focus();
  114.             //激活触发事件
  115.             SettingChanged?.Invoke(this, e);
  116.         }
  117.     }
  118.     #endregion
  119. }
复制代码
【文本显示】
  1. public partial class TextShow : UserControl
  2. {
  3.     public TextShow()
  4.     {
  5.         InitializeComponent();
  6.     }
  7.     #region Fields 变量名称、变量值、单位、字体、控件刻度
  8.     //[Browsable(true)]
  9.     //[Category("布局_G")]
  10.     //[Description("变量名称")]
  11.     //public String VarName { get; set; }
  12.     private Font textFont = new Font("Segoe UI Variable Display", 15, FontStyle.Bold);
  13.     [Browsable(true)]
  14.     [Category("布局_G")]
  15.     [Description("字体格式")]
  16.     public Font TextFont
  17.     {
  18.         get { return textFont; }
  19.         set
  20.         {
  21.             if (value != null)
  22.             {
  23.                 textFont = value;
  24.                 this.lbl_Value.Font = this.lbl_Unit.Font = textFont;
  25.             }
  26.         }
  27.     }
  28.     private Color textColor = Color.Blue;
  29.     [Browsable(true)]
  30.     [Category("布局_G")]
  31.     [Description("文本颜色")]
  32.     public Color TextColor
  33.     {
  34.         get { return textColor; }
  35.         set
  36.         {
  37.             textColor = value;
  38.             this.lbl_Value.ForeColor = this.lbl_Unit.ForeColor = textColor;
  39.         }
  40.     }
  41.     private string varVlaue = "1.0E-5";
  42.     [Browsable(true)]
  43.     [Category("布局_G")]
  44.     [Description("变量值")]
  45.     public string VarVlaue
  46.     {
  47.         get { return varVlaue; }
  48.         set
  49.         {
  50.             varVlaue = value;
  51.             this.lbl_Value.Text = varVlaue;
  52.         }
  53.     }
  54.     private string unit = "Pa";
  55.     [Browsable(true)]
  56.     [Category("布局_G")]
  57.     [Description("单位")]
  58.     public string Unit
  59.     {
  60.         get { return unit; }
  61.         set
  62.         {
  63.             unit = value;
  64.             this.lbl_Unit.Text = unit;
  65.         }
  66.     }
  67.     private float textScale = 0.6f;
  68.     [Browsable(true)]
  69.     [Category("布局_G")]
  70.     [Description("控件刻度")]
  71.     public float TextScale
  72.     {
  73.         get { return textScale; }
  74.         set
  75.         {
  76.             textScale = value;
  77.             this.tableLayoutPanel1.ColumnStyles[0].Width = textScale * this.Width;
  78.             this.tableLayoutPanel1.ColumnStyles[1].Width = this.Width - textScale * this.Width;
  79.         }
  80.     }
  81.     #endregion
复制代码
自定义绘制组件更方便以后直接使用,是一件一劳永逸的事情。
End


来源:https://www.cnblogs.com/guoenshuo/p/18393718
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x

举报 回复 使用道具