Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing many textbox controls in vb.net at once

Tags:

vb.net

textbox

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?

like image 433
Random User Avatar asked Jul 28 '13 07:07

Random User


People also ask

How do you clear in Visual Basic?

Use Clear to explicitly clear the Err object after an error has been handled, for example, when you use deferred error handling with On Error Resume Next. The Clear method is called automatically whenever any of the following statements is executed: Any type of Resume statement. Exit Sub, Exit Function, Exit Property.

What is the purpose of VB net TextBox control?

Typically, a TextBox control is used to display, or accept as input, a single line of text. You can use the Multiline and ScrollBars properties to enable multiple lines of text to be displayed or entered.


1 Answers

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
like image 154
Prash Avatar answered Oct 15 '22 18:10

Prash