Thursday, April 12, 2012

Clear checkboxes inside groupbox not working (bug?)

In a project that i am working with, I have 18 checkboxes. Normally the code below should clear all 18 checkboxes in the groupbox.
Code:
 private void ClearChecklist(GroupBox control)  
     {        
         foreach (Control checklist in control.Controls)  
         {  
           if (checklist is CheckBox)  
           {  
             ((CheckBox)checklist).Checked = false;  
           }  
         }  
     }  

However only 14 checkboxes are deselected. So I modified the code to this:
Code:
private void ClearChecklist(GroupBox control)  
     {  
       if (visited_recursion <= 1)   
       {  
         foreach (Control checklist in control.Controls)  
         {  
           if (checklist is CheckBox)  
           {  
             ((CheckBox)checklist).Checked = false;  
           }  
         }  
         visited_recursion++;  
         ClearChecklist(grpActiveSiteCategories);  
       }  
     }  
I applied a recursion only once to prevent infinite loop/exception. This worked for now.

0 comments:

Post a Comment