【问题标题】:Make text disappear in textbox on click event, and show the same text again when no focus on it?在单击事件时使文本在文本框中消失,并在没有焦点时再次显示相同的文本?
【发布时间】:2013-02-19 10:47:41
【问题描述】:

我在注册表中有 6 个Textbox,其中包含一些预设文本。 当我在Textbox 中单击Name 时,预设文本Enter your full name 应该会消失...如果我随后在Email 中单击Textbox 而不在Name 文本框中写入任何内容,那么Enter your full name 应该再次出现。这个事件应该发生在我所有的Textbox 上,但是它们在每个Textbox 中应该是不同的文本...而不是所有Enter your full name。有人可以帮我吗?

我现在拥有的代码可以在我点击 Textbox 时使用 GotFocus 事件清除它们。

 private void textBox1_GotFocus(Object sender, EventArgs e)
 {
     textBox1.Clear();
 }

在文本框中,我有预设文本....我希望每个文本框的确切预设文本在我单击 Textbox 之外时返回。 我听说过“占位符”?

下面是使用额外构造函数后的样子。我不知道我做错了什么?

 public partial class CustomTextbox : TextBox
 {
    private const string _text = @"Enter your full name";
    private bool _isEmpty = true;

    public CustomTextbox()
    {

        base.ForeColor = SystemColors.GrayText;
        Text = _text;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }
    public CustomTextbox(string tempText)
    {

        base.ForeColor = SystemColors.GrayText;
        Text = tempText;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }

【问题讨论】:

  • 我不明白你的确切问题,但为什么不在 lostfocus 上重新填充?
  • 你能改写你的问题吗?
  • 相信你在找WaterMark,看到这个问题:stackoverflow.com/questions/4902565/…

标签: winforms c#-4.0 textbox onclick


【解决方案1】:

尝试创建一个继承自Textbox 的自定义类。为此,请创建一个新的Usercontrol。删除基类名称并将Textbox 放在那里。如果出现编译错误,请删除设计器中的任何内容。构建您的项目。您应该在工具箱中看到一个新控件。使用它,你就可以开始了。这是Usercontol.cs的代码

public partial class CustomTextbox : TextBox
{
    private const string _text = @"Enter your full name";
    private bool _isEmpty = true;

    public CustomTextbox()
    {
        InitializeComponent();
        base.ForeColor = SystemColors.GrayText;
        Text = _text;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }

    private void TextChangedTextBox(object sender, EventArgs e)
    {
        _isEmpty = string.IsNullOrEmpty(Text);
    }

    public override sealed string Text
    {
        set
        {
            base.Text = value;
        }
    }

    private void LeaveTextBox(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Text))
        {
            Text = _text;
            _isEmpty = true;
            base.ForeColor = SystemColors.GrayText;
        }
        else
        {
            _isEmpty = false;
        }
    }

    private void EnterTextBox(object sender, EventArgs e)
    {
        if (_isEmpty)
            Text = string.Empty;

        base.ForeColor = SystemColors.ControlText;
    }
}

如果您需要任何进一步的信息,请告诉我。当然,您也可以将_text 设为公共属性并使用它来设置所需的文本属性。

希望对你有帮助。

【讨论】:

  • +1 以编程方式设置文本框的 Text 属性时会出现一些问题,但总的来说这看起来是一个不错的解决方案。
  • 嗯...好点。进行了一些更改以反映您的问题。希望它现在应该工作
  • @rapsalands - 这很好用!谢谢!但是,如果我想拥有具有相同效果的多个文本框,但每个文本框中的文本不同......我该如何解决?
  • 阅读答案末尾的注释。我仍然会重复自己....将 _text 作为公共财产。传递文本以设置控件的预设文本。或者你可以简单地通过构造函数传递这个文本属性,你就完成了。希望对您有所帮助。
  • 我现在尝试通过构造函数传递文本并将 _text 作为公共属性。他们都没有给我想要的结果。如果我通过构造函数,文本会消失,但是当失去焦点时“输入你的全名”又回来了?如果我尝试将 _text 作为公共财产,它根本不起作用。您还有其他想法可以让它发挥作用吗?
【解决方案2】:

我解决了我的问题! 感谢@rapsalands 提供代码。 我现在已经根据自己的需要对其进行了修改。

 public partial class UserControl1 : UserControl
 {
    public UserControl1()
    {
        InitializeComponent();
    }
 }

public partial class CustomTextbox : TextBox
{

    private string defaultText;

    public string DefaultText
    {
        get { return defaultText; }
        set { defaultText = value; }
    }


    private bool _isEmpty = true;


    public CustomTextbox(string myText)
    {

        base.ForeColor = SystemColors.GrayText;
        this.Text = myText;
        this.DefaultText = myText;
        Leave += LeaveTextBox;
        Enter += EnterTextBox;
        TextChanged += TextChangedTextBox;
    }


    private void TextChangedTextBox(object sender, EventArgs e)
    {
        _isEmpty = string.IsNullOrEmpty(Text);
    }

    public override sealed string Text
    {
        set
        {
            base.Text = value;
        }
    }

    private void LeaveTextBox(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(Text))
        {
            Text = defaultText;
            _isEmpty = true;
            base.ForeColor = SystemColors.GrayText;
        }
        else
        {
            _isEmpty = false;
        }
    }

    private void EnterTextBox(object sender, EventArgs e)
    {
        if (_isEmpty)
            Text = string.Empty;

        base.ForeColor = SystemColors.ControlText;
    }
}

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多