复选框的问题,期望一个 [英] problem with checkbox to expect one

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

问题描述



我使用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;
                }
            }
        }
    }
}

推荐答案

尝试处理各个检查天数事件:
Try handling the individual days checked event:
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.


这里是解决方案,我对其进行了测试,并且效果很好
-问题在于:-所有事件都在未调用的情况下触发,因此您需要使用焦点"属性来防止事件中的代码触发.


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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆