Results 1 to 15 of 15

Thread: [RESOLVED] Quickest way to clear multiple textboxes

  1. #1

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    [RESOLVED] Quickest way to clear multiple textboxes

    I have around 500 text boxes in my app (yes, I do need them all [for now]). I'd like the user to be able to clear all text boxes in one go. I realise I can use textboxname.Clear() but I really don't want to have 500 individual lines to clear each text box. All text box names are unique. Any help would be appreciated.
    Last edited by MadLadDesigns; Mar 20th, 2025 at 05:37 PM. Reason: [RESOLVED]
    Andy
    [MadLad Designs]

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Quickest way to clear multiple textboxes

    Are all of the textboxes in the same container control (form, panel, GroupBox, etc)?
    Are these textboxes All of your textboxes, or are there some you don’t want to clear?

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Quickest way to clear multiple textboxes

    You can loop through a collection of textboxes.
    If for example, all of your textboxes are on a form, and there are no textboxes to skip…

    Code:
    For Each tb As TextBox in Me.Controls.OfType(Of TextBox)
        tb.Clear
    Next

  4. #4

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    Re: Quickest way to clear multiple textboxes

    Awesome thanks! I have a couple of tab controls, will that clear all boxes within those as well?
    Andy
    [MadLad Designs]

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Quickest way to clear multiple textboxes

    TabControls are different. To start, you can only edit the controls on the selected TabPage. Each TabPage is a container control…
    You can loop through the TextBoxes on TabPage1, like this…

    Code:
    For Each tb As TextBox in Me.TabPage1.Controls.OfType(Of TextBox)
        tb.Clear
    Next

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: Quickest way to clear multiple textboxes

    Quote Originally Posted by .paul. View Post
    TabControls are different. To start, you can only edit the controls on the selected TabPage. Each TabPage is a container control…
    You can loop through the TextBoxes on TabPage1, like this…

    Code:
    For Each tb As TextBox in Me.TabPage1.Controls.OfType(Of TextBox)
        tb.Clear
    Next
    If you want to do that on each TabPage in a TabControl, nest that within a loop over those TabPages:
    Code:
    For Each tp As TabPage In TabControl1.TabPages
        For Each tb In tp.Controls.OfType(Of TextBox)()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    Re: Quickest way to clear multiple textboxes

    I managed to get that all working (also had list boxes and rich text boxes to clear as well), so thank you both, much appreciated.
    Andy
    [MadLad Designs]

  8. #8

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    Re: Quickest way to clear multiple textboxes

    How do I 'skip' text boxes I don't want clearing? I have the following that clears everything:

    Code:
                For Each tp As TabPage In TabMain.TabPages
                    ' Clear all the text boxes in the Adventure Setup tab
                    For Each tb In tp.Controls.OfType(Of TextBox)()
                        tb.Clear()
                    Next
                    ' Clear all the Rich Text Boxes
                    For Each tb In tp.Controls.OfType(Of RichTextBox)()
                        tb.Clear()
                    Next
                    ' Clear all the List Boxes
                    For Each tb In tp.Controls.OfType(Of ListBox)()
                        tb.Items.Clear()
                    Next
                    ' Clear all the Combo Boxes
                    For Each tb In tp.Controls.OfType(Of ComboBox)()
                        tb.Items.Clear()
                        tb.Text = ""
                    Next
                    ' Clear all the NumericUpDowns
                    nudArea.Value = 1
                Next
    Andy
    [MadLad Designs]

  9. #9
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,382

    Re: Quickest way to clear multiple textboxes

    You can check the TextBox.Name property. If possible you could organize all the controls you want cleared/not cleared on a panel or groupbox.

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: Quickest way to clear multiple textboxes

    Code:
    For Each tb In tp.Controls.OfType(Of TextBox).Except(New TextBox() {TextBox1, TextBox2})
    ‘ etc

  11. #11

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    Re: Quickest way to clear multiple textboxes

    Well that was simple! Lol! Thanks .paul
    Andy
    [MadLad Designs]

  12. #12
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,382

    Re: [SOLVED] Quickest way to clear multiple textboxes

    For Each tb In tp.Controls.OfType(Of TextBox).Except(New TextBox() {TextBox1, TextBox2})
    Nice, I had never seen that method before.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,017

    Re: [SOLVED] Quickest way to clear multiple textboxes

    Quote Originally Posted by wes4dbt View Post
    Nice, I had never seen that method before.
    As a part of LINQ, it’s been around since 2008

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,076

    Re: [SOLVED] Quickest way to clear multiple textboxes

    One option to avoid hard-coding specific TextBoxes in your code like .paul. did is to use some special property of the TextBoxes. For instance, you might set the Tag property to "DoNotClear" in the designer for each TextBox you don't want to clear, then do this:
    Code:
    For Each tb In tp.Controls.OfType(Of TextBox).Where(Function(tb) tb.Tag.ToString() <> "DoNotClear")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Member MadLadDesigns's Avatar
    Join Date
    Feb 2025
    Location
    UK
    Posts
    34

    Re: [SOLVED] Quickest way to clear multiple textboxes

    Thanks jmcilhinney.
    Andy
    [MadLad Designs]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width