I am trying to find a code to clear all text boxes in a form. I am using VB8
Here is the code I found but I get an error telling me I need a comma between two arguments

Private Sub btnclearall_Click
Dim ctl As New Control
For Each ctl In Me.Controls
Next
If Typeof ctl Is textbox Then
Dim txtcontrol As Textbox = Directcast (ctl.Textbox)
Me.text = StringEmpty

Thanks

Hello,

Try this

For Each _control As Control In Me.Controls
            If TypeOf (_control) Is TextBox Then
                _control.Text = String.Empty
            End If
        Next

Hi there,

Please try this,

Call ClearContent()
Private Sub ClearContent()
TxtBoxStatus.Text = ""
End sub
Private Sub btnclearall_Click

Dim ctl As New Control

For Each ctl In Me.Controls
Next

'Ok above you looped through all controls on your form but did
'nothing with each itteration

'Now below is only checking one single control, the last one of your form
If Typeof ctl Is textbox Then

    'Above you detemined ctl, is a texbox, so why are
    'you creating a new text box and converting the 
    'the existing textbox into another textbox?
    Dim txtcontrol As Textbox = Directcast (ctl.Textbox)


    'Me is the form you are in, so the only thing
    'you are changing is the text propert of the form itself
    'not any of the text boxes.
    Me.text = StringEmpty

See Rogachev's post above which shows how to correctly itterate thru the controls and clear the textboxes as it goes. But as an additional note, the coding provided will not itterate thru child controls within a container control. For example if you have a few panels and/or group boxes you would need to itterate thru each seperatly to find each of the child controls.

You can use Extension Method - Extension methods enable you to "add" methods to existing types without creating a new derived type, recompiling, or otherwise modifying the original type.

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Controls.ClearControls()

        'To clear Textboxes only
        'Me.Controls.ClearControls(Of TextBox)()
    End Sub
End Class
Public Module Extension

    Private Sub ClearTextBox(ByVal T As TextBox)
        T.Clear()
    End Sub

    Private Sub ClearCheckBox(ByVal T As CheckBox)
        T.Checked = False
    End Sub

    Private Sub ClearListBox(ByVal T As ListBox)
        T.Items.Clear()
    End Sub

    <Runtime.CompilerServices.Extension()> _
    Public Sub ClearControls(ByVal Controls As Control.ControlCollection)
        For Each Control In Controls
            If ControlDefaults.ContainsKey(Control.GetType()) Then
                ControlDefaults(Control.GetType()).Invoke(Control)
            End If
        Next
    End Sub
    <Runtime.CompilerServices.Extension()> _
    Public Sub ClearControls(Of T)(ByVal Controls As Control.ControlCollection)
        If Not ControlDefaults.ContainsKey(GetType(T)) Then Return

        For Each Control In Controls
            If Control.GetType().Equals(GetType(T)) Then
                ControlDefaults(GetType(T)).Invoke(Control)
            End If
        Next
    End Sub
    Private _ControlDefaults As Dictionary(Of Type, Action(Of Control))
    Private ReadOnly Property ControlDefaults() As Dictionary(Of Type, Action(Of Control))
        Get
            If (_ControlDefaults Is Nothing) Then
                _ControlDefaults = New Dictionary(Of Type, Action(Of Control))
                _ControlDefaults.Add(GetType(TextBox), AddressOf ClearTextBox)
                _ControlDefaults.Add(GetType(CheckBox), AddressOf ClearCheckBox)
                _ControlDefaults.Add(GetType(ListBox), AddressOf ClearListBox)
            End If
            Return _ControlDefaults
        End Get
    End Property
End Module
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.