VB.Net .Clear() or txtbox.Text = "" textbox clear methods
Solution 1
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.
Solution 2
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.
Solution 3
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
Solution 4
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)
Solution 5
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.
William Mc
Just new to the world of programming guys so go easy on me! But my name is William, just started computing at the Central Glasgow College and really enjoying it! Hope to be doing Software Development next year! Other side hobbies include playing guitar, football and going to the cinema etc just the usual when away from the computer. Adiós =)
Updated on July 05, 2022Comments
-
William Mc almost 2 years
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.
-
Konrad Rudolph over 13 yearsThe documentation isn’t definite on this so I wouldn’t rely on this behaviour remaining unchanged.
-
Konrad Rudolph over 13 yearsIt looks like somebody at Microsoft had a bad day that day. This looks weird. +1 for digging it out. –1 for Microsoft for crappy documentation (“Clears all the content from the text box” – really?) and weird code.
-
William Mc over 13 yearsThanks very much. Very impressed with the speed of responses. Thanks again!
-
SLaks over 13 years@Konrad: Well, it does. What do you have against the documentation?
-
William Mc over 13 yearsHey guys! I can't accept and check an answer at the moment as, like i said, i am new to programming and still a little unsure about what is going on deep behind the scenes of the code you have all shown me other than clearing the text within the textbox on the form interface. I will defo be digging into my tutor for how these methods tie in with checking null references then i'll come back and rate an answer. Thanks again :)
-
SLaks over 11 years@NeverHopeless: That's not sample code; it's the actual source code from the .Net framework, which is written in C#.
-
NeverHopeless over 11 years@SLaks from
sample
i mean thepiece of code
, I didn't say this apsuedocode
. I know your code and code in question both are .Net framework's code. But it was supposed to be in VB.Net. -
Steve over 7 yearsWhy only two? Let's go with a third one Control.ResetText :-)