Click here to Skip to main content
15,915,850 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I working with checkBox Days , if i checked all days checkbox -> all other days will be checked -> so my problem appears when i want to unchecked to other day -> the all days checkbox shold be changed to unchecked.

here's my code ... plz help to handle this case

C#
private void AllDays_CheckedChanged(object sender, EventArgs e)
{
    if (AllDays.CanFocus)
    {
        if (AllDays.CheckState == CheckState.Checked)
        {
            foreach (Control item in Controls)
            {
                if (item is CheckBox)
                {
                    (item as CheckBox).Checked = true;
                }
            }
        }
        else if (AllDays.CheckState == CheckState.Unchecked)
        {
            foreach (Control item in Controls)
            {
                if (item is CheckBox)
                {
                    (item as CheckBox).Checked = false;
                }
            }
        }
    }
}
Posted
Updated 27-Dec-11 3:33am
v2

Try handling the individual days checked event:
C#
bool isAutoChange = false;
private Void IndividualDay_CheckedChanged(object sender, EventArgs e)
   {
   isAutoChange = true;
   
   AllDays.CheckState = CheckState.UnChecked;

   isAutoChange = false;
   }
Then check the isAutoChange in your AllDays handler and if set, do nothing at all.

The problem is that your AllDays event changes the state of the individual days, and the individual days change the state of AllDays - which causes All Days to fire the event, which...

You need to provide an "override" mechanism to prevent it.
 
Share this answer
 
Comments
[no name] 27-Dec-11 10:10am    
that looks good. And to be on the safe side maybe a try __finally. Anyway 5 from a cp noob.
Regards.
Here's the solution , I test it and it work good
- the problem is that :- all events fire without calling it so you need to use 'Focused' propriety to prevent code in event from firing.


bool checkChanged = false;

       private void AllDays_CheckedChanged(object sender, EventArgs e)
       {
           if (AllDays.Focused)
           {
               if (checkChanged == false)
               {
                   if (AllDays.CheckState == CheckState.Checked)
                   {
                       foreach (Control item in Controls)
                       {
                           if (item is CheckBox)
                           {
                               (item as CheckBox).Checked = true;
                           }
                       }
                   }
                   else if (AllDays.CheckState == CheckState.Unchecked)
                   {
                       foreach (Control item in Controls)
                       {
                           if (item is CheckBox)
                           {
                               (item as CheckBox).Checked = false;
                           }
                       }
                   }
               }
           }


       }

       private void Day4_CheckedChanged(object sender, EventArgs e)
       {
           CheckBox chk = sender as CheckBox;
           if (chk.Focused)
           {
               checkChanged = true;
               if (checkChanged == true)
               {
                   AllDays.CheckState = CheckState.Unchecked;
               }
               checkChanged = false;
           }
       }
 
Share this answer
 
Comments
Honeyboy_20 27-Dec-11 18:57pm    
Great it work with me ...thanks ahmed for your help and thanks also to OriginalGriff

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900