Random User
Random User

Reputation: 365

Clearing many textbox controls in vb.net at once

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?

Upvotes: 5

Views: 38376

Answers (9)

sajjad alzubair
sajjad alzubair

Reputation: 1

This code is tested and the best code I have used:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Controls.Clear()
    InitializeComponent()
    Form1_Load(Me, Nothing)
End Sub

Upvotes: 0

user14410512
user14410512

Reputation: 1

This Will Reset All controls on Form To Default Value

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Controls.Clear()
    InitializeComponent()
    Form1_Load(Me, Nothing)
End Sub

Upvotes: 0

John Mark
John Mark

Reputation: 73

Try this

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

Upvotes: 4

Tim Rossiter
Tim Rossiter

Reputation: 1

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.

I got the idea from 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

Upvotes: 0

Ashraf Elshowen
Ashraf Elshowen

Reputation: 1

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

Upvotes: 0

Miguel Terlaak
Miguel Terlaak

Reputation: 175

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

Upvotes: 4

ku shade
ku shade

Reputation: 9

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.

Upvotes: 0

Prash
Prash

Reputation: 1122

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

Upvotes: 9

Christian Hayter
Christian Hayter

Reputation: 31071

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

Upvotes: 4

Related Questions