扩展Label 标签实现标记必输项*

项目中有许多数据项需要提示用户输入,如果手动增加*号标记是一项十分麻烦的工作,因此将lable扩展根据条件增加*号可以极大的减少重复行工作。
下面的例子中详细的介绍了如何扩展lable标签。


/// <summary> /// 实现lable标签根据条件增加必须项标记 /// </summary> public class CustomLabel : System.Windows.Forms.Label { private bool isNeed; public void InitControl(bool isNeed) { this.isNeed = isNeed; } /// <summary> /// 重写onPaint事件,增加*号标记 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (isNeed) { Graphics acGraphics = null; if (this.Parent != null) { acGraphics = this.Parent.CreateGraphics(); } else { acGraphics = this.CreateGraphics(); } acGraphics.DrawString("*", this.Font, new SolidBrush(Color.Red), this.Location.X + Width - 2, this.Location.Y); } } }

需要的客官可以将Color.red,*号提取成属性,可以在属性面板设置!

效果图

调用代码,根据bool值判断是否增加*号标记

            label23.InitControl(true);
            label21.InitControl(false);
            label22.InitControl(false);

如果对你有帮助点个赞哦

原文地址:https://www.cnblogs.com/houzf/p/12527701.html