Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping through controls on a form

Status
Not open for further replies.

gregburningham

Programmer
Jul 12, 2000
40
0
0
GB
Hi all

I am writing an ASP.NET application and I want to be able to set a number of controls (textboxes and dropdown lists etc.) to property "enabled = false"

This is easy enough but at the moment I am doing this the slow way
ie. TestedDateTextBox.Enabled = False

Is there a quick way of looping through all controls in the form ?

Please can you let me know how this is done ... ?

Thanks
Greg B

 
Public RecursiveSubControls(ctl As Control, Value As Boolean)

Dim ctlInner as Control

For Each ctlInner in ctl.Controls
RecursiveSubControls(ctlInner)
ctlInner.Enabled = Value
Next

End Sub

should be enough to set you on your way....
 
Call the following procedure from the Page_Load event using
Code:
EnableTextBox(Me, False)
:
Code:
Private Sub EnableTextBox(ct As Control, state As Boolean)

    For Each child As Control In ct.Controls

        If child.HasControls Then EnableTextBox(child, state)

        If TypeOf child Is TextBox Then child.Enabled = state

    Next

End Sub
The procedure is recursive to allow for any text boxes within another container (e.g. Panel).
 
Thanks very much for your quick responses - I'll give them a go now !

Greg B


 
Hi

The Routine sent in from SHelton is almost there but for some reason VB.net is not recognising:

the following line :

For Each child As Control In ct.Controls

The "child As Control" is underlined in blue - what am I missing ?


Please help
Thanks again

Greg b









 
The syntax is version 1.1 of the framework - I guess you are using version 1.0 (i.e. 2002).

Add the line
Code:
Dim child As Control
before the loop and change the For Each line to
Code:
For Each child In ct.Controls
.
 
Thanks for that

One more syntax error:

"child.Enabled" property is not allowed for some reason ?


(The rest is now correct)

Greg B




 
Hi,

These methods would work but they are not optimized.
Cycling through controls is a factor that will slow down execution.

Why don't you group these textboxes in a panel and then disable this panel?

bye
 
You can use the controls collection and controls count property to loop through the controls on the form provided they are on the top level of the form.

For I = 0 To MyForm.Controls.Count - 1
'control code here,
Next

But if they are contained in a group box or other container then you have to loop through the container, then the container's container and so on. Below is a routine I use to change set colors of text boxes, combo boxes and list boxes to User options when they are on the top level or within group boxes, tab contros, panels, etc. You could modifiy it to enable and disable your text box by making a few changes.

Public Sub SetControlColor(ByVal X As Form)
Dim I, J, K, M As Integer
Dim T, L, G, P, C, B, V As System.Type
Dim Tbox As New TextBox
Dim Lbox As New ListBox
Dim Cbox As New ComboBox
Dim Pbox As New Panel
Dim Gbox As New GroupBox
Dim Bbox As New TabControl
Dim Vbox As New TabPage
T = Tbox.GetType()
L = Lbox.GetType()
C = Cbox.GetType()
G = Gbox.GetType()
P = Pbox.GetType()
B = Bbox.GetType()
V = Vbox.GetType()
Dim S As String
S = T.ToString
For I = 0 To X.Controls.Count - 1
'on the form itself
setTextListColors(X.Controls(I), T, L, C)
If (X.Controls(I).GetType Is G) Or (X.Controls(I).GetType Is P) Then
'in containers on forms
For J = 0 To X.Controls(I).Controls.Count - 1
If (X.Controls(I).Controls(J).GetType Is G) Or (X.Controls(I).Controls(J).GetType Is P) Then
'in container (G or P) in container (G or P)
For K = 0 To X.Controls(I).Controls(J).Controls.Count - 1
setTextListColors(X.Controls(I).Controls(J).Controls(K), T, L, C)
Next
Else
'in the container (G or P)
setTextListColors(X.Controls(I).Controls(J), T, L, C)
End If

Next
ElseIf (X.Controls(I).GetType Is B) Then
'on tab control
For J = 0 To X.Controls(I).Controls.Count - 1
'on the tab itself
setTextListColors(X.Controls(I).Controls(J), T, L, C)
If (X.Controls(I).Controls(J).GetType Is V) Then
For K = 0 To X.Controls(I).Controls(J).Controls.Count - 1
setTextListColors(X.Controls(I).Controls(J).Controls(K), T, L, C)
If (X.Controls(I).Controls(J).Controls(K).GetType Is G) Or (X.Controls(I).Controls(J).Controls(K).GetType Is P) Then
'in containers on tab controls
For M = 0 To X.Controls(I).Controls(J).Controls(K).Controls.Count - 1
setTextListColors(X.Controls(I).Controls(J).Controls(K).Controls(M), T, L, C)
Next
End If
Next
End If
Next
End If
Next
Tbox.Dispose()
Lbox.Dispose()
Gbox.Dispose()
Pbox.Dispose()
Cbox.Dispose()
Bbox.Dispose()
Vbox.Dispose()
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top