相关疑难解决方法(0)

WinForms中的水印TextBox

任何人都可以指向一个基本的Windows窗体TextBox的良好实现,它最初会显示水印文本,当光标进入时它会消失吗?我想我可以通过创造性地使用Enter和Leave事件来创建我自己的东西,但我确信在某个地方有一个完全可用的实现.我看到了WPF实现,如果有必要,我可以嵌套它,但原生的WinForms TextBox派生会更好.

到目前为止,我有这个; 尚未尝试过,但有没有人看到任何明显的问题?

public class WatermarkTextBox:TextBox
{
    public string WatermarkText { get; set; }

    public Color WatermarkColor { get; set; }

    private Color TextColor { get; set; }

    private bool isInTransition;

    public WatermarkTextBox()
    {
        WatermarkColor = SystemColors.GrayText;
    }

    private bool HasText { get { return Text.IsNotNullOrBlankOr(WatermarkText); }}

    protected override void OnEnter(EventArgs e)
    {
        base.OnEnter(e);

        if (HasText) return;

        isInTransition = true;
        ForeColor = TextColor;
        Text = String.Empty;
        isInTransition = false;
    }

    protected override void OnForeColorChanged(EventArgs e)
    {
        base.OnForeColorChanged(e);
        if (!isInTransition) …
Run Code Online (Sandbox Code Playgroud)

.net textbox watermark winforms

53
推荐指数
5
解决办法
3万
查看次数

标签 统计

.net ×1

textbox ×1

watermark ×1

winforms ×1