Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

Resetting Controls in a Form or Groupbox

Hello HackForums
Gonna share something useful (I hope so)
Got tired of doing this one:

TextBox1.Text = ""
TextBox2.Text = ""

So someone help me and give me this simple snippet. I haven't found something like this here.


Code
Public Sub ResetSomething(frm As Form)

        Dim ctrl As Control
        For Each ctrl In frm.Controls

            'Clearing Textbox
            'Checking if the Control is a TextBox

            If TypeOf ctrl Is TextBox Then
                ctrl.Text = ""
                'All TextBox will be empty.
            End If

            'You can also reset a control inside a panel or groupbox
            If TypeOf ctrl Is GroupBox Then
                Dim cntrl As Control

                For Each cntrl In ctrl.Controls

                    If TypeOf cntrl Is TextBox Then
                        'Just doing the same things
                        cntrl.Text = ""
                    End If

                Next cntrl

            End If


        Next ctrl

    End Sub

I hope it helps you 👍
Code
frm.Controls.OfType(Of TextBox)().ToList().ForEach(Sub(txt) txt.Text.Clear())

Or

Code
For Each txt As TextBox In frm.Controls.OfType(Of TextBox)()
      txt.Text.Clear()
Next
No, it's not useful. It won't be useful for nobody.
(Jul 9, 2015 07:20 AM)LordCoder Wrote:  No, it's not useful. It won't be useful for nobody.

Sorry for posting shit here 😭
(Jul 9, 2015 08:12 AM)Rhez Wrote:  Sorry for posting shit here 😭

At the moment you're only setting the .Text property of textboxes that are directly in your form. If you have textboxes within a groupbox, this code won't work. Update your thread to include a recursive control search, meaning that it will update all controls of a certain type within the child containers as well.

Then it may be helpful to some.
(Jul 9, 2015 04:08 AM)+Haze Wrote:  
Code
frm.Controls.OfType(Of TextBox)().ToList().ForEach(Sub(txt) txt.Text.Clear())

Calling ToList just to access its one liner ForEach? I don't even do this!
Lool, Well Haze does that :)

Edit: What should I do instead? Cast?
(Jul 9, 2015 09:43 AM)+Haze Wrote:  Edit: What should I do instead? Cast?

I.. Oh boy. Leave it be. Your 2nd suggestion was perfectly fine.
(Jul 9, 2015 09:43 AM)+Haze Wrote:  Lool, Well Haze does that :)

Edit: What should I do instead? Cast?
Couldn't you do something along the lines of
Code
frm.Controls.OfType<TextBox>.Select(txt => txt.Text.Clear());
C# cuz you know, C# :> also Pseudo-Code

Edit: Nope nope nope, that won't work, Select is the wrong Linq statement what was it damnit...
(Jul 9, 2015 11:21 AM)Nikey646 Wrote:  Couldn't you do something along the lines of
Code
frm.Controls.OfType<TextBox>.Select(txt => txt.Text.Clear());
C# cuz you know, C# :> also Pseudo-Code

Edit: Nope nope nope, that won't work, Select is the wrong Linq statement what was it damnit...

Finally you've upgraded to leet... what took you so long Nikey. And ask Odus, he's a Linq Guru
Array.ForEach(Me.Controls.OfType(Of TextBox).ToArray, Sub(t) t.Clear())
or
Array.ForEach(Me.Controls.Cast(Of TextBox).ToArray, Sub(t) t.Clear())
(Jul 9, 2015 11:30 AM)+Haze Wrote:  Finally you've upgraded to leet... what took you so long Nikey. And ask Odus, he's a Linq Guru
I've been leet for +1 month.

I recall there being a Linq statement that allows you to modify the source...but cannot remember damnit!

Atlas, i cannot find it and maybe of been thinking of Select, which is ultimately useless. My bad~


Possibly Related Threads...
Thread / Author Replies Views Last Post
by AnonynukSep 28, 2026 10:32 AM 2 43 Sep 28, 2026 10:32 AM
Last Post: Anonynuk
[.NET] by ExproMar 25, 2022 03:58 AM 8 733 Mar 25, 2022 03:58 AM
Last Post: SaketSaket
[.NET] by NaywynJun 7, 2020 12:33 AM 32 3,017 Jun 7, 2020 12:33 AM
Last Post: savaganka