5

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?

Random User
  • 355
  • 4
  • 8
  • 19
  • possible duplicate of [WinForms Clearing multiple Textboxes with one command](http://stackoverflow.com/questions/2217101/winforms-clearing-multiple-textboxes-with-one-command) – Cody Gray - on strike Jul 28 '13 at 13:25
  • 3
    Having textboxes with random names like that introduces a bigger maintenance concern than clearing them using the above method. You need to refactor the names and their hierarchy. Ideally, there should be a reference to all those controls from the parent, so you don't need to recursively traverse Controls tree every time. – Victor Zakharov Jul 28 '13 at 18:45

9 Answers9

9

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
JMK
  • 27,273
  • 52
  • 163
  • 280
Prash
  • 1,122
  • 1
  • 8
  • 10
  • Very streamlined and readable solution in comparison to what I've come across in my research. Allowing the parent to be set as a parameter is a great bonus and the method is furthermore, readily adaptable for including other control types. e.g. [Combo Boxes](http://pastebin.com/wxSFNYeD). – Clarus Dignus Nov 06 '14 at 19:12
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
Christian Hayter
  • 30,581
  • 6
  • 72
  • 99
  • 2
    I'm not entirely convinced that this is better, clearer, or more maintainable than the original code... – Cody Gray - on strike Jul 28 '13 at 13:26
  • The question didn't specify whether clearing all textbox controls or just a specific set was required. Prash answered the former, I answered the latter. It's up to the OP to pick which answer solves the problem. – Christian Hayter Jul 28 '13 at 15:55
4

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
Miguel Terlaak
  • 175
  • 1
  • 13
4

Try this

For Each txt As Control In Me.Controls.OfType(Of TextBox)()
   txt.Text = ""
Next
John Mark
  • 73
  • 11
0

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.

ku shade
  • 9
  • 4
0

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

benc
  • 1,381
  • 5
  • 31
  • 39
0

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

0

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
FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
0

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
Blue
  • 820
  • 4
  • 17