For Each.... Next

  • Thread starter Thread starter june hin via .NET 247
  • Start date Start date
J

june hin via .NET 247

(Type your message here)
Hi. Iam ahving some minor problems regarding for Each...Next. Ihave a form whcih contains a set of radiobuttons in a a groupbox. i have added a button and on clicking the button, i wantall the checkboxes to be set to false. the problem is that iwant to make this happen with for..each Next. it is workingonly when the radio buttons are on the form only, but not whenthey are in the groupbox.

Can anyone of you tell me how to make it work?
 
This is because the radio buttons that are in the group box are not really
on 'the form' but rather they are in the group box. Consider them as being
part of the group boxes controls collection.

Just as you loop through the controls of the form you have to loop through
the controls of the group box because it is a container that can contain
other controls.

Understand?


(Type your message here)
Hi. Iam ahving some minor problems regarding for Each...Next. I have a form
whcih contains a set of radiobuttons in a a group box. i have added a button
and on clicking the button, i want all the checkboxes to be set to false.
the problem is that i want to make this happen with for..each Next. it is
working only when the radio buttons are on the form only, but not when they
are in the groupbox.

Can anyone of you tell me how to make it work?
 
june hin via .NET 247 said:
I have a form whcih contains a set of radiobuttons in a a group box.
i want all the checkboxes to be set to false.
it is working only when the radio buttons are on the form only, but not
when they are in the groupbox.

Not /all/ Controls are now part of the Form; they are nested inside
other container controls, such as GroupBoxes. Each container
Control has a .Controls property that holds references to all the
controls /within/ the container (a Form is a container).

So, to find all the CheckBoxes anywhere /on/ the form or in any
container control on the form, you have to so something a /little/ bit
nasty called Recursion, as in

Private Sub ClearCheckBoxes( ByVal oaContainer as Control )
' Given a CheckBox, clear it
If TypeOf oaContainer Is CheckBox Then
DirectCast( oaContainer, CheckBox).Checked = False
End If

' For any container, search their contents for more checkboxes
For Each eCtl as Control in oaContainer.Controls
ClearCheckBoxes( eCtl )
Next

End Sub

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top