winform文本框怎么实现html的placeholder效果

winfrom默认是不支持这种操作的,此时需要重写控件操作,具体代码如下:

 public class TextBoxEx : TextBox
        {

            public String PlaceHolderStr { get; set; }

            protected override void OnPaint(PaintEventArgs e)
            {
                //
                if (!String.IsNullOrEmpty(this.PlaceHolderStr))
                {
                    //坐标位置 0,0 需要根据对齐方式重新计算.
                    e.Graphics.DrawString(this.PlaceHolderStr, this.Font, new SolidBrush(Color.LightGray), 0, 0);
                }
                else
                {
                    base.OnPaint(e);
                }
            }
        }

  然后在Designer.cs即设计器里面实现该重写的控件

this.ADTextBox = new frmInfoBarCodeFeeDetailed.TextBoxEx();

然后控件属性就会出现所重写的属性了,设置即可

完毕!!!

 原创作品,转载请注明出处!!!

如果您觉得这篇博客对您产生了一些必要的帮助,  欢迎您对我意思意思, 我将会觉得您非常够意思!

原文地址:https://www.cnblogs.com/boosasliulin/p/7767208.html