Is there a way by naming convention say for example to clear all textboxes with a "txt" name prefix in a form that has multiple textboxes? Or, am I going to have to name each textbox individually and use either "" or string.empty?


Thank you in advance for any and all assistance, it is greatly appreciated. :)

try this following code :

Public Sub ClearTextBox(ByVal root As Control)
        For Each ctrl As Control In root.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next ctrl
    End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ClearTextBox(Me)
End Sub

Ok. Hope this helps.

Comments
Great
Worked

Private Sub Command2_Click()
Call clear
End Sub

Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next

Private Sub Command2_Click()
Call clear
End Sub

Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next

your code for vb 6 sonia :)

Your code is right, none wrong with your code. what i mean is if you post in vb.net please use vb.net code, don't used vb6. i know that both of them is still same. but just use right code in current section. please don't mind it... i m sorry if u get upset with.
Happy coding friend :)

Private Sub ClearTextBox(ByVal root As Control)

        For Each cntrl As Control In root.Controls
            ClearTextBox(cntrl)

            If TypeOf cntrl Is TextBox Then
                CType(cntrl, TextBox).Text = String.Empty
            End If
        Next cntrl

End Sub

Edited 6 Years Ago by Nick Evan: code-tags

try this following code :

Public Sub ClearTextBox(ByVal root As Control)
        For Each ctrl As Control In root.Controls
            ClearTextBox(ctrl)
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Text = String.Empty
            End If
        Next ctrl
    End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ClearTextBox(Me)
End Sub

Ok. Hope this helps.

Thanks i do have the same problem ur code worked perfectly

You could as use the StartsWith as well

for each c as control in me.controls
if c.name.startswith("t") then
c.clear()
end if
next

This should cater for all your clearing needs

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

        Dim ctrl, obj As Control

        For Each ctrl In Me.Controls
            If TypeOf ctrl Is GroupBox Then

                For Each obj In ctrl.Controls
                    If TypeOf obj Is TextBox Or TypeOf obj Is ComboBox Or TypeOf obj Is ListBox Then
                        obj.Text = ""
                    End If
                Next
            ElseIf TypeOf ctrl Is TextBox Then
                ctrl.Text = ""
            End If
        Next
    End Sub

Edited 3 Years Ago by mike_2000_17: Fixed formatting

It will save me a lot of time. Earlier I used to write name of each textbox and then set it's text to ""
textbox1.text=""
textbox2.text=""
But I can use this instead :)
Thanks.

Edited 5 Years Ago by NETProgrammer: n/a

The basic thing is...
TextBox.text = ""

Well my brother it is true, For a single textbox and may be for multiple if you write name of each textbox and then set it's value to "" but it's better to use less lines of code.

I don't know if this thread can be re-activated...
I am using the above code to clear all my textboxes before entering a new record.
Now my 'Add new' button is on a separate control on the form. This control includes a search, a gridview where you can select a record, and an 'Add new' button, and is reused for different forms in my application.

When I use: ClearTextBox(Me), it only clears the textbox in the control, not in the form.
How can I set it up to clear all text boxes in the form.

Thank you for your help

Instead of ClearTextBox(Me) write the form name in which u want to clear the text boxes
like

ClearTextBox(Form1)

I found the answer:
ClearTextBox(me.parent)
Poojavb, your answer only works if you know the name of the form, as this control will be used on many forms, I cannot name the form.
Thanks for your help.

This article has been dead for over six months. Start a new discussion instead.