Donate

Clear Checkboxes Inside GroupBox Not Working In C# Windows Forms

In a project that i am working with, I have 18 checkboxes. Normally the code below should clear all 18 checkboxes in the groupbox.
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:
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.

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.