如何实现显示"在此处输入"的TextBox?

maf*_*afu 39 c# textbox winforms

显示" 在此处键入... ",直到用户将文本输入到a中,这TextBox是当今众所周知的可用性功能.如何在C#中实现此功能?

我的想法是覆盖OnTextChanged,但处理文本更改的逻辑" 在这里输入 "有点棘手......

在初始化时显示" 在此输入 "并在第一次输入时将其删除很容易,但我想在每次输入的文本变空时显示消息.

Poo*_*ven 41

对我有用的东西:

this.waterMarkActive = true;
this.textBox.ForeColor = Color.Gray;
this.textBox.Text = "Type here";

this.textBox.GotFocus += (source, e) =>
  {
    if (this.waterMarkActive)
    {
      this.waterMarkActive = false;
      this.textBox.Text = "";
      this.textBox.ForeColor = Color.Black;
    }
  };

this.textBox.LostFocus += (source, e) =>
  {
    if (!this.waterMarkActive && string.IsNullOrEmpty(this.textBox.Text))
    {
      this.waterMarkActive = true;
      this.textBox.Text = "Type here";
      this.textBox.ForeColor = Color.Gray;
    }
  };
Run Code Online (Sandbox Code Playgroud)

bool waterMarkActive类成员变量在哪里,textBoxTextBox.这可能应该封装虽然:)这种方法可能存在一些问题,但我目前还没有意识到.

我最近发现Windows支持文本框中的水印; 他们被称为提示横幅(见这里).它很容易实现:

// Within your class or scoped in a more appropriate location:
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)] string lParam);

// In your constructor or somewhere more suitable:
SendMessage(textBox.Handle, 0x1501, 1, "Please type here.");
Run Code Online (Sandbox Code Playgroud)

其中textBox是的一个实例TextBox,0x1501是用于Windows消息的代码EM_SETCUEBANNER,所述wParam可以是TRUE(非零)或FALSE(零),并且lParam是要显示水标记.wParam指示何时应显示提示横幅; 如果设置为,TRUE那么即使控件具有焦点,也会显示提示横幅.


Mar*_*ona 25

您正在寻找的是带有" 水印 " 的texbox

有对C#示例实现在这里.

希望能帮助到你


Joe*_*oel 8

基于@Pooven的回答(谢谢!),我创建了这个类.适合我.

/// <summary>
/// A textbox that supports a watermak hint.
/// </summary>
public class WatermarkTextBox : TextBox
{
    /// <summary>
    /// The text that will be presented as the watermak hint
    /// </summary>
    private string _watermarkText = "Type here";
    /// <summary>
    /// Gets or Sets the text that will be presented as the watermak hint
    /// </summary>
    public string WatermarkText
    {
        get { return _watermarkText; }
        set { _watermarkText = value; }
    }

    /// <summary>
    /// Whether watermark effect is enabled or not
    /// </summary>
    private bool _watermarkActive = true;
    /// <summary>
    /// Gets or Sets whether watermark effect is enabled or not
    /// </summary>
    public bool WatermarkActive
    {
        get { return _watermarkActive; }
        set { _watermarkActive = value; }
    }

    /// <summary>
    /// Create a new TextBox that supports watermak hint
    /// </summary>
    public WatermarkTextBox()
    {
        this._watermarkActive = true;
        this.Text = _watermarkText;
        this.ForeColor = Color.Gray;

        GotFocus += (source, e) =>
        {
            RemoveWatermak();
        };

        LostFocus += (source, e) =>
        {
            ApplyWatermark();
        };

    }

    /// <summary>
    /// Remove watermark from the textbox
    /// </summary>
    public void RemoveWatermak()
    {
        if (this._watermarkActive)
        {
            this._watermarkActive = false;
            this.Text = "";
            this.ForeColor = Color.Black;
        }
    }

    /// <summary>
    /// Applywatermak immediately
    /// </summary>
    public void ApplyWatermark()
    {
        if (!this._watermarkActive && string.IsNullOrEmpty(this.Text)
            || ForeColor == Color.Gray ) 
        {
            this._watermarkActive = true;
            this.Text = _watermarkText;
            this.ForeColor = Color.Gray;
        }
    }

    /// <summary>
    /// Apply watermak to the textbox. 
    /// </summary>
    /// <param name="newText">Text to apply</param>
    public void ApplyWatermark(string newText)
    {
        WatermarkText = newText;
        ApplyWatermark();
    }

}
Run Code Online (Sandbox Code Playgroud)


小智 5

  [DllImport("user32.dll", CharSet = CharSet.Auto)]
  private static extern Int32 SendMessage(IntPtr hWnd, int msg, int wParam, [MarshalAs(UnmanagedType.LPWStr)]string lParam);
  const int EM_SETCUEBANNER = 0x1501; 

  public Form1()
  {
      InitializeComponent();
      SendMessage(textBox1.Handle, EM_SETCUEBANNER, 1, "Username");
      SendMessage(textBox2.Handle, EM_SETCUEBANNER, 1, "Password");
  }
Run Code Online (Sandbox Code Playgroud)