Old ladyHow Big Is Your Package?
Size up and convert your DTS package to SSIS. If you have the biggest DTS package, win a gift card and certificate of a world record package size.  
Clear or Reset all textbox controls on a VB .net Form - Mike Davis
in

Pragmatic Works

Mike Davis

Clear or Reset all textbox controls on a VB .net Form

When you have a reset button on a vb form and you want that button to clear all of the text box fields, you have two options. Write code for each field to clear them or loop through each control on the form.

Here is an example of clearing each field individually:

Private Sub resetForm()

       ServerName.text = ""
       PortNumber.text = ""
      and so on ....

End Sub

As you can see this give you complete control of each control individually. But if you have dozens of fields this can be a headache. Also, maintaining this becomes a hassle. Each time you add a control you have to update the resetform method.

Here is the loop through example:

Private Sub resetForm()

    'Set an integer to loop through the controls
    Dim x As Integer
    ' Set the loop for one less than the count because the fields start their index at 0
    For x = 0 To Me.Controls.Count - 1

       'if it is a text box then clear it 
        If TypeOf Me.Controls.Item(x) Is TextBox Then
            Me.Controls.Item(x).Text = ""
        End If

    Next

End Sub

As you can see, this method never needs to be updated. You can add or remove controls to your form and the method with react accordingly.

You could also do the same thing for radio buttons, check boxes or any other editable field. You just need a case for each type of control.

 Hope this helps someone.

Comments

No Comments

About Mike Davis

Mike Davis, is a developer, consultant, trainer, and mentor who is enthusiastic about developing robust application for SQL server. He has expertise in many areas of Business Intelligence including Integration Services, Reporting Services, Database Administration, and .Net Software Development. Mike has created BI and software solutions for financial institutions and pragmatic works. He has developed .Net applications for SQL add-ons as well as standalone applications. Mike also participates as a speaker at events like SQL Server 2008 launch and SQL server user group meetings .
Copyright Pragmatic Works
Powered by Community Server (Non-Commercial Edition), by Telligent Systems