VB.Net .Clear() or txtbox.Text = “” textbox clear methods

后端 未结 8 773
囚心锁ツ
囚心锁ツ 2021-01-11 13:49

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

相关标签:
8条回答
  • 2021-01-11 14:25
    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)
    
    0 讨论(0)
  • 2021-01-11 14:25

    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.

    0 讨论(0)
  • 2021-01-11 14:30

    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.

    0 讨论(0)
  • 2021-01-11 14:36

    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.

    0 讨论(0)
  • 2021-01-11 14:36

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

    0 讨论(0)
  • 2021-01-11 14:41

    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
    
    0 讨论(0)
提交回复
热议问题