將佔位符新增到文字框
此程式碼將提示文字放在表單載入中並按如下方式操作它:
C#
private void Form_load(object sender, EventArgs e)
{
textBox.Text = "Place Holder text...";
}
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.Trim() == "")
{
textBox.Text = "Place Holder text...";
}
}
VB.NET
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
textBox.Text = "Place Holder text..."
End Sub
Private Sub textBox_GotFocus(sender as Object,e as EventArgs) Handles textBox.GotFocus
if Trim(textBox.Text) = "Place Holder text..." Then
textBox.Text = ""
End If
End Sub
Private Sub textBox_LostFocus(sender as Object,e as EventArgs) Handles textBox.LostFocus
if Trim(textBox.Text) = "" Then
textBox.Text = "Place Holder text..."
End If
End Sub