我有6个Textbox
的注册表与一些预设的文本。当我单击用于Name
的Textbox
,然后单击preset text Enter your full name
should disappear...If,然后单击用于Email
的Textbox
,而不在Name
文本框中写入任何内容,然后Enter your full name
应该再次出现。此事件应该发生在所有my Textbox
上,但它们在所有Textbox
...Not Enter your full name
中的文本应该不同。有没有人能帮我一下?
我现在拥有的代码使我可以使用GotFocus事件在单击Textbox
时清除它们。
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;
}
发布于 2013-02-19 23:01:13
尝试创建一个继承自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;
}
}
如果你需要任何进一步的信息,请告诉我。
希望能有所帮助。
发布于 2013-02-21 16:44:14
我解决了我的问题!感谢@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;
}
}
}
https://stackoverflow.com/questions/14955546
复制相似问题