How to can i count All Control in form?

isawa

Active member
Joined
Oct 3, 2005
Messages
25
Programming Experience
Beginner
How to can i count All Control in form and make it to ReadOnly?
:confused:
 
dim i as integer = 0
for each Crtl as control in me.controls
ctrl.readonly = true
i +=1
next

This is assuming of course that the controls on your from all have a readonly property.
 
Note that that will work as long as you have no container controls, like GroupBoxes, Panels, etc. The following will work in those cases too. Also, ReadOnly applies to TextBoxes and RichTextBoxes only. With Option Strict turned On (as I strongly suggest it always is) you would have to cast the control as at least a TextBoxBase reference:
VB.NET:
        Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control.
        Dim ctlCount As Integer = 0

        While Not ctl Is Nothing
            ctlCount += 1

            If TypeOf ctl Is TextBoxBase Then
                CType(ctl, TextBoxBase).ReadOnly = True
            End If

            ctl = Me.GetNextControl(ctl, True)
        End While

        MessageBox.Show(ctlCount.ToString() & " controls found.")
 
I puted that code to my UserControl don't error and don't do anything, but when put it to my window form work well. How can i put that code to my usercontrol for work well? show me some code. Thanks,

Public Sub BY_MarkReadOnly(ByVal vReadOnly As Boolean)
Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control.
Dim ctlCount As Integer = 0
While Not ctl Is Nothing
ctlCount += 1
If TypeOf ctl Is TextBoxBase Then
CType(ctl, TextBoxBase).ReadOnly = vReadOnly
End If
ctl = Me.GetNextControl(ctl, True)
End While
End Sub

 
It works fine for me. Note that if you put that code in a UserControl it will only affect TextBoxes within that UserControl. It will not affect other TextBoxes on the form. That's because it calls Me.GetNextControl, where 'Me' refers to the UserControl. If you want it to affect the entire form regardless of where it's called then you'd need to replace 'Me' with 'Me.TopLevelControl':
VB.NET:
        Dim ctl As Control = Me.TopLevelControl.GetNextControl(Me.TopLevelControl, True) 'Get the first control.
        Dim ctlCount As Integer = 0

        While Not ctl Is Nothing
            ctlCount += 1

            If TypeOf ctl Is TextBoxBase Then
                CType(ctl, TextBoxBase).ReadOnly = True
            End If

            ctl = Me.TopLevelControl.GetNextControl(ctl, True)
        End While
 
Good stuff JMCILHINNEY.... I didn't even know that TypOf and TopLeveControl were methods you could use. This saves me a headache b/c I tried hard to find a way to differentiate between different controls. Also, now I know how to search every control in the form jus as so and add a recursive call to get all controls. Or at least I think I can get that to work.
 
ImDaFrEaK said:
Good stuff JMCILHINNEY.... I didn't even know that TypOf and TopLeveControl were methods you could use. This saves me a headache b/c I tried hard to find a way to differentiate between different controls. Also, now I know how to search every control in the form jus as so and add a recursive call to get all controls. Or at least I think I can get that to work.
If you want to differentiate between a few different types of controls here's a trick a learned recently:
VB.NET:
        For Each ctl As Control In Me.Controls
            Select Case True
                Case TypeOf ctl Is TextBox
                    Dim myTextBox As TextBox = DirectCast(ctl, TextBox)
                Case TypeOf ctl Is Label
                    Dim myLabel As Label = DirectCast(ctl, Label)
            End Select
        Next
Note the structure of the Select Case statement. This is useful in any situation where you can't use the expression you want in a Select Case.
 
Thanks again. That's exactly what I had in mind. Actually, I want to use that code and when I run into a control that's a panel for example call the procedure again. I am going to write a procedure that does this but in a module and I have to send it one argument (object). I will send it (me) when I call it at the class level and when I hit a panal control in the select case I will recall the procedure (recursive) and send it control. I think this will allow me to check every control on the entire form whether they are in panels or groupbox's ect... What do you think? I am not at my computer at home to test this.
 
You don't have to worry about calling anything recursively. GetNextControl follows the tab order and takes into account nested controls. That's the very reason that it's preferable to iterating over the form's Controls collection. Try setting up a form with GroupBoxes and Panels on TabPages with other controls on the form, on the TabPages and in the other containers. Then use that code from within the form and have it popup a MessageBox displaying the Name of each control. You'll find that it visits every control on the form, no matter how deep it's nested.
 
Dang, That's even better. Thanks again. I did not know this and in the book I was studying it never even mentioned that method much less explain it. I really appreciate it and that saves me a headache.
 
Back
Top