此代碼清除文本框,如果當前值的佔位符值,否則它保留了輸入值。
Public Class Form1
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "0.00" Then
TextBox1.Text = ""
End If
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then
TextBox1.Text = "0.00"
End If
End Sub
End Class
如果你總是希望它來清除文本框,然後用這個。
Public Class Form1
Private Sub TextBox1_GotFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
TextBox1.Text = ""
End Sub
Private Sub TextBox1_LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then
TextBox1.Text = "0.00"
End If
End Sub
End Class
爲了模擬實際的佔位符,如一個在HTML5你需要覆蓋你的文本框的頂部label
控制,並設置它是基於事件keyDown
和LostFocus
/Leave
事件visability這將取決於你的版本VS
我一直在尋找一種方式,當你開始輸入清除文本框,但我耗盡了時間。您可以設置第一個擊鍵標誌並保留擊鍵或在進入文本框後全選。 – ZL1Corvette