1,105,333 Community Members

clear ALL textboxes

Member Avatar
SolTec
Junior Poster in Training
66 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
5
 

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.

Member Avatar
sonia sardana
Posting Whiz
329 posts since Mar 2008
Reputation Points: -13 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
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

your code for vb 6 sonia :)

Member Avatar
sonia sardana
Posting Whiz
329 posts since Mar 2008
Reputation Points: -13 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
0
 

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

Member Avatar
sonia sardana
Posting Whiz
329 posts since Mar 2008
Reputation Points: -13 [?]
Q&As Helped to Solve: 8 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
PiasaJohnny
Newbie Poster
2 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
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
Member Avatar
anupama96
Newbie Poster
3 posts since Aug 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
msrd
Newbie Poster
14 posts since Aug 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
morrisgit
Newbie Poster
1 post since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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
Member Avatar
NETProgrammer
Light Poster
49 posts since May 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
Juily63430
Newbie Poster
1 post since May 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
NETProgrammer
Light Poster
49 posts since May 2011
Reputation Points: -3 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Pie46
Newbie Poster
2 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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

Member Avatar
poojavb
Posting Pro
524 posts since Nov 2011
Reputation Points: 29 [?]
Q&As Helped to Solve: 78 [?]
Skill Endorsements: 9 [?]
 
0
 

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

ClearTextBox(Form1)
Pie46
Newbie Poster
2 posts since Jun 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

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.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article