一次清除 vb.net 中的许多文本框控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17906065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:02:39  来源:igfitidea点击:

Clearing many textbox controls in vb.net at once

vb.nettextbox

提问by Random User

I am using the following code to clear the

我正在使用以下代码清除

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()

Is there any possibility to clear all the textbox controls at once or with few lines of code?

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

回答by Prash

You can iterate over all controls on the form which are contained in root.Controlsand 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

您可以遍历表单中包含的所有控件root.Controls并查看它是否属于文本框类型TypeOf ctrl Is TextBox,然后您可以清除该控件中的文本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

回答by Christian Hayter

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

回答by Miguel Terlaak

I tried one of the examples here but this seems to work for me:

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

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

回答by John Mark

Try this

尝试这个

For Each txt As Control In Me.Controls.OfType(Of TextBox)()
   txt.Text = ""
Next

回答by Ashraf Elshowen

This is my code that I personally use it in my projects ^_^

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

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

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

回答by Tim Rossiter

I had to cast the Control as a TextBox to make this work. Something about the control object not being accessible. There may be a more direct way but I was unable to find one.

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

I got the idea from https://stackoverflow.com/a/16264904/11242863

我的想法来自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

I hope this helps someone,

我希望这可以帮助别人,

Tim R

蒂姆·R

回答by ku shade

try this one

试试这个

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

or

或者

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

if .Clear() is a member of textbox property then use it. i guess .clear() is not a member of textbox properties.

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