VB.Net .Clear() or txtbox.Text = "" textbox clear methods
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.
txtbox1.Clear()
or
txtbox1.Text = ""
Any help is much appreciated.
The Clear
method is defined as
public void Clear() {
Text = null;
}
The Text
property's setter starts with
set {
if (value == null) {
value = "";
}
I assume this answers your question.
The two methods are 100% equivalent.
I’m not sure why Microsoft felt the need to include this extra Clear
method but since it’s there, I recommend using it, as it clearly expresses its purpose.
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:
Private Sub Command1_Click()
Call ClearTextBoxes(Me)
End Sub
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)
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.
Just use:TextBox1.Clear()
It will work fine.
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
Specifically if you want to clear your text box in VB.NET or VB 6.0, write this code:
TextBox1.Items.Clear()
If you are using VBA, then the use this code :
TextBox1.Text = ""
or
TextBox1.Clear()
精彩评论