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.

commented: Worked +1
commented: Great +1

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

hi jx_man, your code for vb 6 sonia.
what it maens???
Is it not a rite code?????????

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

I don't mind it yaar. It's by mistake.

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

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

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.

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.