清除vb.net中tabcointrols中的文本框

时间:2017-06-03 17:22:18

标签: vb.net textbox tabcontrol

我有三个标签的tabcontrol,每个标签包含文本框,我想清除每个标签中的所有文本框,只需点击一下按钮(按钮事件或任何其他方式清除所有文本框)

我已经尝试过很多代码但只对第一个标签有效

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

2 个答案:

答案 0 :(得分:0)

这看起来应该更像:

repaint()

答案 1 :(得分:0)

这是另一种设置为语言扩展方法的方法。创建一个新的代码模块,并用下面的代码替换默认代码。

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

清除TabControl1中的所有TextBox和ComboBox控件。

TabControl1.ClearSpecificControls

清除表单上的所有控件

ClearSpecificControls