如何在 VB.Net 2010 中像 html 一样使用文本框的占位符 [英] how to use placeholder for Textbox in VB.Net 2010 like html
问题描述
我有一个表格可以进行一些数学计算.
I have a form that does some mathematical calculation.
我希望用户能够快速输入数据而不会擦除值.
I want to be able users to enter data quickly without erasing the value.
我觉得像在 html 中那样使用占位符很好
I thing its good to use placeholder like I did in html
但是我如何在 VB.Net 2010 中使用它?谢谢
but how can i use it in VB.Net 2010? Thanks
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
gtp.Text = "0.00"
vatt.Text = "0.00"
wht.Text = "0.00"
npr.Text = "0.00"
End Sub
推荐答案
如果当前值为占位符值,此代码清除文本框,否则保留输入值.
This code clears the text box if the current value is the placeholder value, otherwise it retains the input value.
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
如果您总是希望它清除文本框,请使用它.
If you always want it to clear the textbox then use this.
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
事件,这取决于你的 VS 版本
To simulate an actual placeholder such as the one in HTML5 you'll need to overlay a label
control on top of your textbox and set it's visability based on the event keyDown
and LostFocus
/Leave
Event which will depend on your version of VS
这篇关于如何在 VB.Net 2010 中像 html 一样使用文本框的占位符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!