文本框中的灰色描述性文本,单击后消失
Gray descriptive text in a textbox which disappears once clicked
我希望文本框本身在文本框中包含单词 "Insert Description",一旦用户单击文本框开始输入,"Insert Description" 就会消失,他们可以输入文本。
下面是我所说的一个例子:
请注意文本框中有灰色 "Search"。我怎样才能做到这一点?
如果是 WPF,请尝试以下操作:
通过告诉 XAML 您正在绑定到一个不存在的 属性 作弊,然后在 属性 的 PlaceHolderValue 中设置您的占位符文本:
<TextBox Name="myTextBox" VerticalAlignment="Top"
Text="{Binding Path=fakeproperty, PlaceHolderValue='Insert Description'}"/>
Winforms,试试这个:
private const string PLACE_HOLDER_TEXT = "Insert Description";
private void textBox1_MouseEnter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text)) textBox1.Text = PLACE_HOLDER_TEXT;
}
private void Form_load(object sender, EventArgs e)
{
textBox.Text = "Place Holder text...";`enter code here`
}
private void textBox_Enter(object sender, EventArgs e)
{
if(textBox.Text == "Place Holder text...")
{
textBox.Text = ""
}
}
private void textBox_Leave(object sender, EventArgs e)
{
if(textBox.Text == "")
{
textBox.Text = "Place Holder text..."
{
}
文本框不包含占位符属性所以你必须自己这样做
我希望文本框本身在文本框中包含单词 "Insert Description",一旦用户单击文本框开始输入,"Insert Description" 就会消失,他们可以输入文本。
下面是我所说的一个例子:
请注意文本框中有灰色 "Search"。我怎样才能做到这一点?
如果是 WPF,请尝试以下操作:
通过告诉 XAML 您正在绑定到一个不存在的 属性 作弊,然后在 属性 的 PlaceHolderValue 中设置您的占位符文本:
<TextBox Name="myTextBox" VerticalAlignment="Top"
Text="{Binding Path=fakeproperty, PlaceHolderValue='Insert Description'}"/>
Winforms,试试这个:
private const string PLACE_HOLDER_TEXT = "Insert Description";
private void textBox1_MouseEnter(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(textBox1.Text)) textBox1.Text = PLACE_HOLDER_TEXT;
}
private void Form_load(object sender, EventArgs e)
{
textBox.Text = "Place Holder text...";`enter code here`
}
private void textBox_Enter(object sender, EventArgs e)
{
if(textBox.Text == "Place Holder text...")
{
textBox.Text = ""
}
}
private void textBox_Leave(object sender, EventArgs e)
{
if(textBox.Text == "")
{
textBox.Text = "Place Holder text..."
{
}
文本框不包含占位符属性所以你必须自己这样做