paulopulus
paulopulus

Reputation: 221

A effective method for resetting controls Text and Values on a form?

I have some controls on a Form and don't want to set the values to String.Empty or whatever the default is for all of them manually using TextBoxes,DateTimePickers, ComboboxList etc.

I want a method that "cleans" all my controls for fresh new data.

Upvotes: 0

Views: 825

Answers (2)

dbasnett
dbasnett

Reputation: 11773

Something like this perhaps

    'this loop will get all the controls on the form
    'no matter what the level of container nesting

    Dim ctrl As Control = Me.GetNextControl(Me, True)
    Do Until ctrl Is Nothing
        'set defaults based on type
        Select Case True
            Case TypeOf ctrl Is TextBox
                Stop 'set defaults
            Case TypeOf ctrl Is Label
                Stop 'set defaults
            Case TypeOf ctrl Is Button
                Stop 'set defaults
        End Select
        ctrl = Me.GetNextControl(ctrl, True)
    Loop

Upvotes: 0

Douglas Barbin
Douglas Barbin

Reputation: 3615

I would recommend iterating through all of the controls of whichever type you want to reset. For example:

For Each tb In myForm.Controls.OfType(Of TextBox)
    tb.Text = String.Empty
Next tb

For Each cb In myForm.Controls.OfType(Of CheckBox)
    ' Clear out your checkboxes here
Next cb

etc...

Upvotes: 0

Related Questions