-
Feb 27th, 2025, 05:56 PM
#1
Thread Starter
Member
[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]
-
Feb 27th, 2025, 07:36 PM
#2
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?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2025, 07:41 PM
#3
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 27th, 2025, 08:55 PM
#4
Thread Starter
Member
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?
-
Feb 27th, 2025, 09:15 PM
#5
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 28th, 2025, 02:36 AM
#6
Re: Quickest way to clear multiple textboxes
 Originally Posted by .paul.
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)()
-
Feb 28th, 2025, 02:19 PM
#7
Thread Starter
Member
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.
-
Mar 1st, 2025, 02:08 PM
#8
Thread Starter
Member
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
-
Mar 1st, 2025, 02:21 PM
#9
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.
-
Mar 1st, 2025, 02:22 PM
#10
Re: Quickest way to clear multiple textboxes
Code:
For Each tb In tp.Controls.OfType(Of TextBox).Except(New TextBox() {TextBox1, TextBox2})
‘ etc
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 1st, 2025, 02:28 PM
#11
Thread Starter
Member
Re: Quickest way to clear multiple textboxes
Well that was simple! Lol! Thanks .paul
-
Mar 1st, 2025, 05:26 PM
#12
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.
-
Mar 1st, 2025, 05:34 PM
#13
Re: [SOLVED] Quickest way to clear multiple textboxes
 Originally Posted by wes4dbt
Nice, I had never seen that method before.
As a part of LINQ, it’s been around since 2008
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 2nd, 2025, 12:32 AM
#14
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")
-
Mar 20th, 2025, 05:36 PM
#15
Thread Starter
Member
Re: [SOLVED] Quickest way to clear multiple textboxes
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|