5

我正在使用以下代码清除

txtint1.Clear()
txtext1.Clear()
txttot1.Clear()
txtint2.Clear()
txtext2.Clear()
txttot2.Clear()
txtint3.Clear()
txtext3.Clear()
txttot3.Clear()
txtint4.Clear()
txtext4.Clear()
txttot4.Clear()
txtint5.Clear()
txtext5.Clear()
txttot5.Clear()
txtint6.Clear()
txtext6.Clear()
txttot7.Clear()
txtint8.Clear()
txtext8.Clear()
txttot8.Clear()

是否有可能一次或几行代码清除所有文本框控件?

4

9 回答 9

7

You can iterate over all controls on the form which are contained in root.Controls and see if it is of type a textbox TypeOf ctrl Is TextBox, then you can clear the text in that control CType(ctrl, TextBox).Text = String.Empty

Well!! You need to use recursion to loop through all controls

Adding the code:

Public Sub ClearTextBox(parent As Control)

    For Each child As Control In parent.Controls
        ClearTextBox(child)
    Next

    If TryCast(parent, TextBox) IsNot Nothing Then
        TryCast(parent, TextBox).Text = [String].Empty
    End If

End Sub
于 2013-07-28T07:47:18.153 回答
4

You could put them in an array then loop through the array:

For Each txt In {txtint1, txtext1, txttot1, txtint2, txtext2, txttot2, txtint3, txtext3, txttot3, txtint4, txtext4, txttot4, txtint5, txtext5, txttot5, txtint6, txtext6, txttot7, txtint8, txtext8, txttot8}
    txt.Clear()
Next
于 2013-07-28T07:47:35.857 回答
4

我在这里尝试了其中一个示例,但这似乎对我有用:

Dim a As Control
    For Each a In Me.Controls
        If TypeOf a Is TextBox Then
            a.Text = Nothing
        End If
    Next
于 2017-10-15T15:08:27.137 回答
3

尝试这个

For Each txt As Control In Me.Controls.OfType(Of TextBox)()
   txt.Text = ""
Next
于 2020-01-23T06:25:31.073 回答
0

这会将表单上的所有控件重置为默认值

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Controls.Clear()
    InitializeComponent()
    Form1_Load(Me, Nothing)
End Sub
于 2020-10-08T00:42:19.647 回答
0

我必须将 Control 转换为 TextBox 才能完成这项工作。关于无法访问控制对象的某些内容。可能有更直接的方法,但我找不到。

我从https://stackoverflow.com/a/16264904/11242863得到了这个想法

Private Sub clearAllTextBoxes()
    Dim formControl As Control
    Dim txtBox As TextBox
    For Each formControl In Me.Controls
        If TypeOf formControl Is TextBox Then
            txtBox = TryCast(formControl, TextBox)
            txtBox.Clear()
        End If
    Next
End Sub

我希望这可以帮助别人,

蒂姆·R

于 2019-03-22T15:31:39.703 回答
0

此代码经过测试,是我使用过的最佳代码:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Controls.Clear()
    InitializeComponent()
    Form1_Load(Me, Nothing)
End Sub
于 2021-02-20T16:34:59.767 回答
0

这是我个人在项目中使用的代码^_^

ClearAll TextBoxes : For Each TxtBox In Me.Controls.OfType(Of TextBox) TxtBox.Clear() Next TxtBox

于 2019-01-14T21:24:59.307 回答
0

试试这个

    Dim a As Control
    For Each a In Me.Controls
        If TypeOf a Is TextBox Then
            a.Text = ""
        End If
    Next

或者

Dim a As Control
    For Each a In Me.Controls
        If TypeOf a Is TextBox Then
            a.clear()
        End If
    Next

如果 .Clear() 是文本框属性的成员,则使用它。我猜 .clear() 不是文本框属性的成员。

于 2016-04-17T03:41:16.133 回答