Link to home
Start Free TrialLog in
Avatar of Larry Rungren
Larry RungrenFlag for United States of America

asked on

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
Avatar of Larry Rungren
Larry Rungren
Flag of United States of America image

ASKER

this is to clear text boxes in selective panels
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

ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Works!

THANKS