如何在 VB.Net 2010 中像 html 一样使用文本框的占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21457737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to use placeholder for Textbox in VB.Net 2010 like html
提问by usminuru
I have a form that does some mathematical calculation.
我有一个表格可以进行一些数学计算。
I want to be able users to enter data quickly without erasing the value.
我希望用户能够快速输入数据而不会擦除值。
I thing its good to use placeholder like I did in html
我觉得像我在 html 中那样使用占位符很好
but how can i use it in VB.Net 2010? Thanks
但是如何在 VB.Net 2010 中使用它?谢谢
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
回答by ZL1Corvette
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
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
要模拟实际的占位符,例如 HTML5 中的占位符,您需要label
在文本框顶部覆盖一个控件,并根据事件keyDown
和LostFocus
/Leave
事件设置其可见性,这取决于您的 VS 版本
回答by Gabriel Intriago
I created DLL for this work.
我为这项工作创建了 DLL。
https://1drv.ms/u/s!AmR1BM6vUcAGgYsLPvjrec0Z92OTlQ
https://1drv.ms/u/s!AmR1BM6vUcAGgYsLPvjrec0Z92OTlQ
Or you can download the project PlaceHolder class
或者你可以下载项目PlaceHolder类
https://1drv.ms/u/s!AmR1BM6vUcAGgYsMNFGbtW5HL_4Ifw
https://1drv.ms/u/s!AmR1BM6vUcAGgYsMNFGbtW5HL_4Ifw
How to use?
如何使用?
In your winform project add the DLL.
在您的 winform 项目中添加 DLL。
(I added groupbox for this example)
(我为这个例子添加了 groupbox)
CS Code:
编码:
var textBoxWithPlaceHolder = new Placeholder.PlaceholderTextBox();
textBoxWithPlaceHolder.PlaceholderText = "Search text";
textBoxWithPlaceHolder.Location = new Point(x: 10, y: 20);
GBTextBox.Controls.Add(textBoxWithPlaceHolder);
Result winform
结果winform