store objects into array

yellowwing12

New member
Joined
Aug 3, 2005
Messages
4
Programming Experience
3-5
I have 12 existing textboxes on the form. Is there anyway I can store them to an array and them use a loop to clear all of their contents instead of using:

textbox1.text = ""
textbox2.text = ""
textbox3.text = ""
...so on.
 
VB.NET:
[/color][/size]
[size=2][color=#0000ff]Dim[/color][/size][size=2] xControl [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size][size=2][color=#008000]'you can declare the object as textbox and than you shouldn't check the control's type
[/color][/size][size=2][color=#0000ff]For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] xControl [/size][size=2][color=#0000ff]In [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] xControl.GetType [/size][size=2][color=#0000ff]Is [/color][/size][size=2][color=#0000ff]GetType[/color][/size][size=2](System.Windows.Forms.TextBox) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]xControl.text = [/size][size=2][color=#0000ff]String[/color][/size][size=2].Empty
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Next


Cheers ;)

Also you could take a look at the attached project below :)
 

Attachments

  • GetTypeAndClear.zip
    24 KB · Views: 38
One way would be to also check if the control in the loop is a GroupBox. If so, loop through the GroupBox's Control collection to reach the nested textBoxs. A recursive function would work.
 
How would I do that? I dont know what a recursive function is (unless we are talking about the math graphing kind) so could you expain?
 
VB.NET:
For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] xControl [/size][size=2][color=#0000ff]In[/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].GroupBox1.Controls
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] xControl.GetType [/size][size=2][color=#0000ff]Is [/color][/size][size=2][color=#0000ff]GetType[/color][/size][size=2](System.Windows.Forms.TextBox) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]xControl.text = [/size][size=2][color=#0000ff]String[/color][/size][size=2].Empty
 
[/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Next

Happy coding ;)
 
Here's an example of a recursive function that would clear all textboxs in the given control (forms are derived from controls), even those nested many layers deep:
VB.NET:
Private Sub ClearTextBoxs(ByVal ctrl As Control)
    Dim xControl As Control
    For Each xControl In ctrl.Controls
        If TypeOf xControl Is TextBox Then
            Dim txtbx As TextBox = CType(xControl, TextBox)
            txtbx.Text = String.Empty
        ElseIf xControl.HasChildren Then
            ClearTextBoxs(xControl)
        End If
    Next
End Sub
notice that the function calls itself in the cases where the control in the loop has childControls, hence recursive.
It is possible to code this without typecasting the variable, but not with Option Strict On.
 
Back
Top