Return 焦点到文本框中的占位符

Return focus to placeholder in text box

我有一个登录表单。我有一个带有登录和关闭按钮的用户名和密码文本框。

我愿意:

我试过这个:

Private Sub Form_Load()
txt_username.Value = "Username"
txt_password.Value = "Password"
End Sub

Private Sub txt_username_Click()
If txt_username.Value = "Username" Then
txt_username.Value = ""
End If
End Sub

Private Sub txt_username_LostFocus()
If txt_username.Value = "" Then
txt_username.Value = "Username"
End If
End Sub

我的问题是在进行更改时,即您键入某些内容,然后将其删除,在您执行其他操作后占位符不会 return。打字会破坏我的代码。

你很接近。尝试使用“GotFocus”而不是“Click”:

Option Explicit

Private Sub Form_Load()
   txt_username.Text = "Username"
   txt_password.Text = "Password"
End Sub

Private Sub txt_username_GotFocus()
   If Trim(txt_username.Text) = "Username" Then
      txt_username.Text = ""
   End If
End Sub

Private Sub txt_username_LostFocus()
   If Trim(txt_username.Text) = "" Then
      txt_username.Text = "Username"
   End If
End Sub