【问题标题】:Textbox in c# windows Formc# windows窗体中的文本框
【发布时间】:2012-01-23 21:41:44
【问题描述】:

我的表单中有一个文本框。在表单加载事件中,我想要我的 textbox.text = "Enter Name" 但它应该看起来好像单击了文本框,文本框的文本消失了。 附图将帮助您理解我的问题:)

【问题讨论】:

标签: c# textbox


【解决方案1】:

【讨论】:

  • 您在 google 搜索中找到了同一个站点,或者您正在复制在 previous SO answer 上的引用。如果是后者,请务必在您的回答中引用作者的帖子。
  • @BradChristie 好点。我用那个帖子标记了这个 q,不妨也把它贴在这里。
  • 另外,我这里还有一个using WndProc(如果作者不介意的话)。
【解决方案2】:

使用 TextBox.ForeColor 并将其更改为灰色并使用 textBox 的 Enter/Leave 事件更改颜色并删除文本

【讨论】:

    【解决方案3】:

    这是一个不使用DllImport 的解决方案

    /// <summary>
    /// inspired by this forum entry: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/10f75954-6d14-4926-a02d-98649653b9c8/
    /// Watermark TextBox in winform
    /// </summary>
    public class WatermarkTextBox : TextBox
    {
      private string watermarkText;
      private Color watermarkColor;
      private Color foreColor;
      private bool isTextBoxEmpty;
    
      public WatermarkTextBox()
      {
        this.WatermarkText = "Watermark Text...";
        this.WatermarkColor = Color.FromKnownColor(KnownColor.Silver);
        this.Enter += onEnter;
        this.Leave += onLeave;
      }
    
      [Browsable(true)]
      public new Color ForeColor
      {
        get { return this.foreColor; }
        set
        {
          if (value == this.foreColor)
          {
            return;
          }
          this.foreColor = value;
          if (!this.isTextBoxEmpty)
          {
            base.ForeColor = value;
          }
        }
      }
    
      [Browsable(true)]
      public Color WatermarkColor
      {
        get { return this.watermarkColor; }
        set
        {
          if (value == this.watermarkColor)
          {
            return;
          }
          this.watermarkColor = value;
          if (this.isTextBoxEmpty)
          {
            base.ForeColor = this.watermarkColor;
          }
        }
      }
    
      [Browsable(true)]
      public string WatermarkText
      {
        get { return this.watermarkText; }
        set
        {
          if (value == this.watermarkText)
          {
            return;
          }
          this.watermarkText = value;
          if (base.Text.Length == 0)
          {
            this.isTextBoxEmpty = true;
            base.Text = this.watermarkText;
            base.ForeColor = this.watermarkColor;
          }
          this.Invalidate();
        }
      }
    
      public override string Text
      {
        get { return this.isTextBoxEmpty ? string.Empty : base.Text; }
        set
        {
          if (string.IsNullOrEmpty(value))
          {
            this.isTextBoxEmpty = true;
            base.ForeColor = this.watermarkColor;
            base.Text = this.watermarkText;
          }
          else
          {
            this.isTextBoxEmpty = false;
            base.ForeColor = this.foreColor;
            base.Text = value;
          }
        }
      }
    
      private void onEnter(object sender, EventArgs e)
      {
        if (this.isTextBoxEmpty)
        {
          this.isTextBoxEmpty = false;
          base.ForeColor = this.foreColor;
          base.Text = string.Empty;
        }
      }
    
      private void onLeave(object sender, EventArgs e)
      {
        if (string.IsNullOrEmpty(base.Text))
        {
          this.isTextBoxEmpty = true;
          base.ForeColor = this.watermarkColor;
          base.Text = this.watermarkText;
        }
        else
        {
          this.isTextBoxEmpty = false;
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-23
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2011-05-11
      • 1970-01-01
      • 2013-05-31
      • 1970-01-01
      相关资源
      最近更新 更多