clear text fields after insert with loop or array

danomatic

Member
Joined
Oct 26, 2002
Messages
7
Location
Boston, MA
I have a basic form that inserts text fields into a database. When I click insert, I would like the fields to all clear.

I know how to do it one line at a time but it would be more efficient if I could do a loop or an array but I am not sure how. Here is what I have now. (and it works)
Code:
txtAddress.Text = ""
(....and 10 more fields like this)
thx, dan

NOTE: I am new to VB and VB.Net only within the past few weeks but with Oracle/SQL Server background.

Version: VS Studio.Net 2002 Enterprise Architect
 
You can use this short snippet of code to clear all the text boxes on a form (or other container control). The code recurses on itself so if you have textboxes in another container control on your form (a groupbox for example) they are cleared too.

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

    Private Sub ClearTextBoxes(ByVal parent As Control)
        Dim c As Control

        For Each c In parent.Controls
            If TypeOf c Is TextBox Then
                c.Text = ""
            ElseIf c.Controls.Count <> 0 Then
                ClearTextBoxes(c)
            End If
        Next
    End Sub
 
Back
Top