首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否在单击事件时使文本在文本框中消失,并在没有焦点时再次显示相同的文本?

是否在单击事件时使文本在文本框中消失,并在没有焦点时再次显示相同的文本?
EN

Stack Overflow用户
提问于 2013-02-19 18:47:41
回答 2查看 2.5K关注 0票数 1

我有6个Textbox的注册表与一些预设的文本。当我单击用于NameTextbox,然后单击preset text Enter your full name should disappear...If,然后单击用于EmailTextbox,而不在Name文本框中写入任何内容,然后Enter your full name应该再次出现。此事件应该发生在所有my Textbox上,但它们在所有Textbox...Not Enter your full name中的文本应该不同。有没有人能帮我一下?

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

代码语言:javascript
运行
复制
 private void textBox1_GotFocus(Object sender, EventArgs e)
 {
     textBox1.Clear();
 }

在文本框中,我有预置文本...我希望每当我在Textbox之外单击时,每个文本框中的预置文本都会返回。我听说过什么“占位符”?

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

代码语言:javascript
运行
复制
 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;
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-02-19 23:01:13

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

代码语言:javascript
运行
复制
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;
    }
}

如果你需要任何进一步的信息,请告诉我。

希望能有所帮助。

票数 2
EN

Stack Overflow用户

发布于 2013-02-21 16:44:14

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

代码语言:javascript
运行
复制
 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;
    }
}

}

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14955546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档