Groups | Blog | Home
all groups > vb.net controls > may 2006 >

vb.net controls : Lock textbox with a Group



MATT
5/3/2006 3:02:01 PM
I have simple code that i use to make each textbox in a form "Read Only":
For Each ctl In frm.Controls
If TypeOf ctl Is TextBox Then
txb = ctl
txb.ReadOnly = True
txb.BackColor = Color.White
End If
Next ctl
This code worked fine until I added a Group Control to the form. Now, if I
step through the code, I can see that the ctl never "gets to" the textboxses,
but it does the group control. If, in the form code, I type "formName." all
of the textbox controls appear. If if view "frm.contrls.count" it is clear
that the textboxes are not being included in this collection. Could someone
explain the Textboxes are excluded from this collection when i add the group
Jay B. Harlow [MVP - Outlook]
5/7/2006 5:57:28 PM
MATT,
It sounds like you placed your TextBox on the GroupBox control.

Which means your Form contains a GroupBox, and your GroupBox contains a
TextBox control.

You need to recursively look at the controls that are children of the Group
control, something like:

Private Sub SetReadOnly(ByVal parent As Control)
For Each child As Control In parent.Controls
If TypeOf child Is TextBox Then
Dim txb As TextBox = DirectCast(child, TextBox)
txb.ReadOnly = True
txb.BackColor = Color.White
End If
If child.HasChildren Then SetReadonly(child)
Next
End Sub

Then within your form you could call it with:

SetReadOnly(Me)

As Form inherits from Control.

Which recursively looks at the children of all controls searching out
TextBoxes...

--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley - http://www.tsbradley.net


[quoted text, click to view]
|I have simple code that i use to make each textbox in a form "Read Only":
| For Each ctl In frm.Controls
| If TypeOf ctl Is TextBox Then
| txb = ctl
| txb.ReadOnly = True
| txb.BackColor = Color.White
| End If
| Next ctl
| This code worked fine until I added a Group Control to the form. Now, if
I
| step through the code, I can see that the ctl never "gets to" the
textboxses,
| but it does the group control. If, in the form code, I type "formName."
all
| of the textbox controls appear. If if view "frm.contrls.count" it is
clear
| that the textboxes are not being included in this collection. Could
someone
| explain the Textboxes are excluded from this collection when i add the
group
| control to the form.

AddThis Social Bookmark Button