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 diff
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.
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.
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.
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()
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