水印在文本框的WinForms水印、文本框、WinForms

2023-09-02 10:15:14 作者:一抹浅念丶

任何人都可以点我一个很好的执行的基本Windows窗体文本框最初将显示,当光标进入它消失的水印文字?我想我可以创造我自己的一些创造性地使用Enter和Leave事件,但我敢肯定有一个非常有用的实施坐在某处。我看到了WPF实现,并且如果有必要我可以窝,但本机的WinForms文本框衍生效果会更好。

Can anyone point me to a good implementation of a basic Windows Forms TextBox that will initially show watermark text that disappears when the cursor enters it? I think I can create my own with some creative use of Enter and Leave events, but I'm sure there's a perfectly usable implementation sitting around somewhere. I saw the WPF implementation and if necessary I could nest it, but a native WinForms TextBox derivative would be better.

我有这样为止;还没有尝试过,但没有人看到任何明显的问题?

I have this so far; haven't tried it yet but does anyone see any glaring problems?

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) //the change came from outside
            TextColor = ForeColor;
    }

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

        if (HasText) return;

        isInTransition = true;
        ForeColor = WatermarkColor;
        Text = WatermarkText.EmptyIfNull();
        isInTransition = false;
    }
}

编辑:上面会最终曾与一些finessing,但CueProvider工作要好得多。这是我的最终实现:

The above would have eventually worked with some finessing, but the CueProvider worked much better. Here's my final implementation:

public class WatermarkTextBox:TextBox
{
    private string watermarkText;
    public string WatermarkText
    {
        get { return watermarkText; }
        set
        {
            watermarkText = value;
            if (watermarkText.IsNullOrBlank())
                CueProvider.ClearCue(this);
            else
                CueProvider.SetCue(this, watermarkText);
        }
    }
}

我可以完全集成的CueProvider的功能,但这种精美的作品。

I could have integrated the CueProvider functionality completely, but this works beautifully.

推荐答案

官方的术语是提示的旗帜。这里是另一种方式来做到这一点,只是继承文本框得到执行过任务。添加一个新类到您的项目并粘贴下面所示的code。编译。从工具箱的顶部掉落新的控制,并设置提示属性。

The official term is "cue banner". Here's another way to do it, just inheriting TextBox gets the job done too. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox and set the Cue property.

您得到设计师的线索价值,本地化的窗体的语言属性的现场preVIEW。很多爆炸的很少降压,的WinForms的好的部分的一个很好的示范。

You get a live preview of the Cue value in the designer, localized to the form's Language property. Lots of bang for very little buck, an excellent demonstration of the good parts of Winforms.

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class CueTextBox : TextBox {
    [Localizable(true)]
    public string Cue {
        get { return mCue; }
        set { mCue = value; updateCue(); }
    }

    private void updateCue() {
        if (this.IsHandleCreated && mCue != null) {
            SendMessage(this.Handle, 0x1501, (IntPtr)1, mCue);
        }
    }
    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        updateCue();
    }
    private string mCue;

    // PInvoke
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, string lp);
}