0

I have tabcontrol of three tabs and each tabs contains textbox and i want to clear all textboxes in each tabs and a one click of button(button event or any alternate way to clear all textboxes)

i have already tried many code however that only effective for first tab only

Private Sub ClearFields(ByVal cntr As Control)
    For Each page As TabPage In TabControl1.TabPages
        For Each ctl As Control In page.Controls
            If TypeOf ctl Is TextBox Then
                ctl.Text = ""
            End If
            If TypeOf ctl Is ComboBox Then
                ctl.Text = ""
            End If
            If ctl.HasChildren Then
                For Each thing As Control In ctl.Controls
                    If TypeOf thing Is TextBox Then
                        thing.Text = ""
                    End If
                Next
            End If
        Next
    Next
End Sub
babin
  • 1
  • 1

2 Answers2

1

That should look something more like:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ClearTabControl(TabControl1)
End Sub

Private Sub ClearTabControl(ByVal tb As TabControl)
    For Each page As TabPage In TabControl1.TabPages
        ClearFields(page)
    Next
End Sub

Private Sub ClearFields(ByVal cntr As Control)
    For Each ctl As Control In cntr.Controls
        If TypeOf ctl Is TextBox OrElse TypeOf ctl Is ComboBox Then
            ctl.Text = ""
        ElseIf ctl.HasChildren Then
            ClearFields(ctl)
        End If
    Next
End Sub
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

Here is another method which is setup as a language extension method. Create a new code module and replace the default code with the code below.

Public Module ControlExtensions
    <Runtime.CompilerServices.Extension()>
    Public Sub ClearSpecificControls(ByVal sender As Control)
        Dim currentControl As Control = sender.GetNextControl(sender, True)
        Dim cboControl As ComboBox = Nothing

        Do Until currentControl Is Nothing
            If TypeOf currentControl Is TextBox Then
                currentControl.Text = ""
            ElseIf TypeOf currentControl Is ComboBox Then
                cboControl = CType(currentControl, ComboBox)
                If cboControl.DataSource IsNot Nothing Then
                    cboControl.DataSource = Nothing
                Else
                    cboControl.Items.Clear()
                End If
            End If
            currentControl = sender.GetNextControl(currentControl, True)
        Loop
    End Sub
End Module

Clear all TextBox and ComboBox controls within TabControl1.

TabControl1.ClearSpecificControls

Clear all controls on a form

ClearSpecificControls
Karen Payne
  • 4,341
  • 2
  • 14
  • 31