Connecting Tech Pros Worldwide Help | Site Map

Clear all checkboxes at form load

Newbie
 
Join Date: Feb 2009
Posts: 9
#1: Feb 4 '09
Hi,

I have a pile of checkboxes on one particular form and would like to clear them all at the form load rather that saying chk1.Checked = False for all of them.

Here is the code I was trying to use but 'checked' is not a member of Systems.Windows.Forms.Controls.
Expand|Select|Wrap|Line Numbers
  1.     Dim ctl As Control
  2.         For Each ctl In Me.Controls
  3.             If TypeOf ctl Is CheckBox Then
  4.                 ctl.checked = False
  5.             End If
  6.         Next ctl
  7.  
How can I fix this??
Thanks.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,148
#2: Feb 4 '09

re: Clear all checkboxes at form load


Well how did they get checked in the first place?
When a form loads it reads the values from what you set in the designer.
So at design time make sure they are all false?


Otherwise your sample code is almost right, you would just need to check inside container controls (GroupBox for example) and check their child controls to see if they are checkboxes.
As for your error, your are not casting the control as a CheckBox before trying to use the Checked proeprty
Newbie
 
Join Date: Feb 2009
Posts: 9
#3: Feb 5 '09

re: Clear all checkboxes at form load


Thanks.
Sorted now
Reply