clearing text boxes in multiple panels on a vb2010 form

i have searched around for a method to clear all text boxes on multiple panels and the general consensus seems to me that this will work.


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim a As Control
        For Each a In Me.GroupBox1.Controls
            If TypeOf a Is TextBox Then
                a.Text = Nothing
            End If
        Next
    End Sub

Open in new window



unfortunately it doesn't clear any text boxes at all.

i did see one post that referred to an issue if there is a menustrip, which i do have on the form, but it wasn't clear
Jeff_KingstonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jeff_KingstonAuthor Commented:
this is to clear text boxes in selective panels
0
Jeff_KingstonAuthor Commented:
I found a block that does work.  republish for any other newbies trying to convert old vb6 code

 Public Sub ClearTextBoxes(Optional ByVal ctlcol As Control.ControlCollection = Nothing)
        If ctlcol Is Nothing Then ctlcol = Me.Controls
        For Each ctl As Control In ctlcol
            If TypeOf (ctl) Is TextBox Then
                DirectCast(ctl, TextBox).Clear()
            Else
                If Not ctl.Controls Is Nothing OrElse ctl.Controls.Count <> 0 Then
                    ClearTextBoxes(ctl.Controls)
                End If
            End If
        Next
    End Sub

Open in new window

0
Mike TomlinsonMiddle School Assistant TeacherCommented:
An alternate version as an extension method of Control:
Public Module Extensions

	<Runtime.CompilerServices.Extension()>
	Public Sub ClearTextBoxes(ByVal containerControl As Control)
		For Each childControl As Control In containerControl.Controls
			If TypeOf childControl Is TextBox Then
				childControl.ResetText()
			ElseIf childControl.HasChildren Then
				childControl.ClearTextBoxes()
			End If
		Next
	End Sub

End Module

Open in new window


Allowing you to call it like this:
Public Class Form1

	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
		Me.ClearTextBoxes()
	End Sub

End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jeff_KingstonAuthor Commented:
Works!

THANKS
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.