Checking all checkboxes and clearing all textboxes with single loop.

litesh80

Newcomer
Joined
Aug 25, 2005
Messages
5
I have 20 check boxes in a form.
and I have 20 textboxes in another form.
now my requirement goes as follows.
On clicking a button all the checkboxes should go checked or uncheked.
All this should happen in one loop.

The same requirement goes for clearing textboxes in next form.

Suggestions are appreciated.
 
For Loop

To do so in a loop is possible, but I can't recall how to. :D You'll need to do so using a for loop.

Despite unable to recall, I did a solution (see attached file). Though it seems quite long, it's just one of the many ways that work.

I'd be glad if someone could come up with a solution to this problem too. I'd like to find out about it too....
 

Attachments

Last edited by a moderator:
Visual Basic:
dim Chk as checkbox
for each chk in me.controls
chk.checked = true
next chk

Visual Basic:
      Dim chk As New CheckBox
        For x As Integer = 0 To Me.Controls.Count - 1
            If Me.Controls(x).GetType Is chk.GetType Then
                CType(Me.Controls(x), CheckBox).Checked = True
            End If
        Next
        chk.Dispose()

Both work.

By the way: In the future, take the .exe's out of your attachments (There's one in the bin folder and one in another folder). I'm not sure about this forum, but most forums don't allow attachments that include .exe's.
 
Or to throw another way:
Visual Basic:
 Dim c As System.Windows.Forms.Control
        For Each c In Me.Controls
            If TypeName(c) = "CheckBox" Then
                CType(c, CheckBox).Checked = True
            End If
        Next

:)
 
None of ur methods worked...

Thank you for ur response.
But Unfortunately I could not work out with either ways u have mentioned.
Provide some better way.
 
Not working

It's not working...

mark007 said:
Or to throw another way:
Visual Basic:
 Dim c As System.Windows.Forms.Control
        For Each c In Me.Controls
            If TypeName(c) = "CheckBox" Then
                CType(c, CheckBox).Checked = True
            End If
        Next

:)
 
Second one worked fine when i did it in a new window.
Now I'll have to try it on the original project.
Anyway I'll check it out.
Thank you again.
 
All are working fine when the Controls are directly on to the form.
But I am using 5 groupboxes in which i am placing the controls.
Its not working....
 
Or use recursion to get all the controls:

Visual Basic:
CheckBoxes(Me)

Visual Basic:
Private Sub CheckBoxes(ByVal Parent As System.Windows.Forms.Control)
        dim c As System.Windows.Forms.Control
        For Each c In Parent.Controls
            CheckBoxes(c)
            If TypeName(c) = "CheckBox" Then
                CType(c, CheckBox).Checked = True
            End If
        Next 
End Sub

:)
 
Back
Top