Winforms 在单击事件时使文本在文本框中消失,并在没有焦点时再次显示相同的文本?
我在注册表中有6个带有一些预设文本的Winforms 在单击事件时使文本在文本框中消失,并在没有焦点时再次显示相同的文本?,winforms,c#-4.0,textbox,onclick,Winforms,C# 4.0,Textbox,Onclick,我在注册表中有6个带有一些预设文本的文本框。 当我点击文本框查看姓名时,预设文本输入您的全名应消失……如果我随后点击文本框查看电子邮件而未在姓名文本框中写入任何内容,则输入您的全名应再次出现。此事件应该发生在所有my文本框中,但每个文本框中的文本应该不同,而不是在所有文本框中输入您的全名。有人能帮我吗 我现在拥有的代码使我可以使用GotFocus事件在单击文本框时清除它们 private void textBox1_GotFocus(Object sender, EventArgs e) {
文本框
。
当我点击文本框
查看姓名
时,预设文本输入您的全名
应消失……如果我随后点击文本框
查看电子邮件
而未在姓名
文本框中写入任何内容,则输入您的全名
应再次出现。此事件应该发生在所有my文本框中,但每个文本框中的文本应该不同,而不是在所有文本框中输入您的全名。有人能帮我吗
我现在拥有的代码使我可以使用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;
}
尝试创建从文本框继承的自定义类。为此,请创建一个新的Usercontrol
。删除基类名称并将Textbox
放在那里。如果出现编译错误,请删除设计器中的任何内容。构建您的项目。您应该在工具箱中看到一个新控件。使用它,你就可以很好地去做。以下是usercontrol.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;
}
}
如果你需要任何进一步的信息,请告诉我。作为not,您还可以将\u text
作为公共属性,并使用它设置所需的文本属性
希望有帮助。我解决了我的问题!
感谢@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;
}
}
}我不明白您的确切问题,但为什么不重新填充lostfocus?您能重新表述您的问题吗?我相信您正在寻找水印,请参见此问题:+1以编程方式设置Textbox的Text属性时可能会出现一些问题,但总的来说,这看起来是一个不错的解决方案。嗯……很好。进行了一些更改以反映您的问题。希望它能起作用now@rapsalands-这很有效!谢谢但是,如果我想有多个具有相同效果的文本框,但每个文本中有不同的文本…我如何解决这个问题?请阅读答案末尾的注释。但我还是要重复我自己……把文本作为公共财产。传递文本以设置控件的预设文本。或者你可以简单地通过构造函数传递这个文本属性,你就完成了。希望有帮助。我现在已经尝试了通过构造函数传递文本和将文本作为公共属性。他们都没有给我想要的结果。如果我使用构造函数,文本会消失,但是当失去焦点时,“输入您的全名”会回来吗?如果我尝试将文本作为公共属性,它根本不起作用。你还有什么其他的想法让它工作吗?