本文介绍了复选框的问题,期望一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用checkBox Days,如果我勾选了all days checkbox->所有其他日期都将被选中->所以当我想取消选中其他日期时出现我的问题->全天复选框保留更改为未选中.

这是我的代码...请帮助处理这种情况

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

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;
                }
            }
        }
    }
}

推荐答案

bool isAutoChange = false;
private Void IndividualDay_CheckedChanged(object sender, EventArgs e)
   {
   isAutoChange = true;
   
   AllDays.CheckState = CheckState.UnChecked;

   isAutoChange = false;
   }

然后在AllDays处理程序中检查isAutoChange,如果已设置,则什么也不做.

问题在于您的AllDays事件会更改各个日期的状态,而单个日期会更改AllDays的状态-这会导致All Days触发该事件,从而导致...
您需要提供一种替代"机制来防止它.

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.


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;
           }
       }


这篇关于复选框的问题,期望一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:34