VB.Net .Clear() 或 txtbox.Text = "" 文本框清除方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3754108/
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
VB.Net .Clear() or txtbox.Text = "" textbox clear methods
提问by William Mc
Not far into programming and just joined this forum of mighty company so this is a silly question, but what is the best way to clear textboxes in VB.Net and what is the difference between the two methods? I have also seen people be critical of folk using clear objects on their forms and I can see why but in this case, I am only learning.
编程不远,刚刚加入这个强大公司的论坛,所以这是一个愚蠢的问题,但是在 VB.Net 中清除文本框的最佳方法是什么,这两种方法有什么区别?我也看到人们批评人们在他们的形式上使用清晰的物体,我明白为什么,但在这种情况下,我只是在学习。
txtbox1.Clear()
or
或者
txtbox1.Text = ""
Any help is much appreciated.
任何帮助深表感谢。
采纳答案by Konrad Rudolph
The two methods are 100% equivalent.
这两种方法是 100% 等效的。
I'm not sure why Microsoft felt the need to include this extra Clearmethod but since it's there, I recommend using it, as it clearly expresses its purpose.
我不知道为什么微软觉得需要包含这个额外的Clear方法,但既然它在那里,我建议使用它,因为它清楚地表达了它的目的。
回答by SLaks
The Clearmethod is defined as
该Clear方法定义为
public void Clear() {
Text = null;
}
The Textproperty's setter starts with
该Text属性的setter开头
set {
if (value == null) {
value = "";
}
I assume this answers your question.
我假设这回答了你的问题。
回答by Suhas Nila
Add this code in the Module :
在模块中添加此代码:
Public Sub ClearTextBoxes(frm As Form)
For Each Control In frm.Controls
If TypeOf Control Is TextBox Then
Control.Text = "" 'Clear all text
End If
Next Control
End Sub
Add this code in the Form window to Call the Sub routine:
在 Form 窗口中添加以下代码以调用 Sub 例程:
Private Sub Command1_Click()
Call ClearTextBoxes(Me)
End Sub
回答by Hossam Ali
Public Sub EmptyTxt(ByVal Frm As Form)
Dim Ctl As Control
For Each Ctl In Frm.Controls
If TypeOf Ctl Is TextBox Then Ctl.Text = ""
If TypeOf Ctl Is GroupBox Then
Dim Ctl1 As Control
For Each Ctl1 In Ctl.Controls
If TypeOf Ctl1 Is TextBox Then
Ctl1.Text = ""
End If
Next
End If
Next
End Sub
add this code in form and call this function
在表单中添加此代码并调用此函数
EmptyTxt(Me)
回答by Hossam Ali
Just use:TextBox1.Clear()It will work fine.
只需使用:TextBox1.Clear()它会正常工作。
回答by user7479740
If u want to Selected text clear then using to this code i will make by my self ;)
如果您想清除选定的文本,则使用此代码我将自己制作;)
If e.KeyCode = Keys.Delete Then
TextBox1.SelectedText = ""
End If
thats it
就是这样
回答by Brian Mains
Clear() set the Text property to nothing. So txtbox1.Text = Nothing does the same thing as clear. An empty string (also available through String.Empty) is not a null reference, but has no value of course.
Clear() 将 Text 属性设置为空。所以 txtbox1.Text = Nothing 和 clear 一样。空字符串(也可通过 String.Empty 获得)不是空引用,但当然没有值。
回答by Shyam Bhattacharyya
Specifically if you want to clear your text box in VB.NET or VB 6.0, write this code:
具体来说,如果要清除 VB.NET 或 VB 6.0 中的文本框,请编写以下代码:
TextBox1.Items.Clear()
TextBox1.Items.Clear()
If you are using VBA, then the use this code :
如果您使用的是 VBA,则使用以下代码:
TextBox1.Text = ""or
TextBox1.Clear()
TextBox1.Text = ""或者
TextBox1.Clear()

