复选框验证 [英] Check box validation

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

问题描述

我有三个复选框,我需要验证,以便您只能发送其中一个。我有代码,所以你只能选择其中一个文本框(代码看起来像这样)



I've got three checkboxes and I need validation so that you can only text one of them. I've got the code so you can only select one of the text boxes (that code looks like this)

private void toc_Blockleave_CheckedChanged(object sender, EventArgs e)
       {
           if (toc_Blockleave.Checked == true)
           {
               toc_Swap.Checked = false; toc_Unavailability.Checked = false;
           }







但是我需要它以便当点击btn_Submit时你必须有一个选中的复选框(您只能根据上面的代码选择一个)并返回一个Messagebox。



这就是我的尝试






however I need it so that when btn_Submit is clicked you must have one of the tickboxes selected (you can only select one due to the above code) and it returns a Messagebox.

This is what I tried

if (!(rdo_ApplySelectedCodeToRD.Checked & rdo_LeaveRDAlone.Checked & rdo_SetRDToZR.Checked))
            {
                MessageBox.Show("You must choose a Rest Day Option");
                return;
            }







以及其他各种方法,但我无法得到它工作






as well as various other methods but I just can't get it work

if (toc_Blockleave.Checked & toc_Swap.Checked & toc_Unavailability.Checked == false )
            {
                MessageBox.Show("You must choose a Type of Change");
                return;
            }







我试过这个,它只适用于其中一个盒子而不是第二个。



谢谢:3




I tried this and it only worked for one of the boxes and not the toher two.

Thanks :3

推荐答案

我只使用一个事件处理程序复选框(顺便说一下,你知道单选按钮在这里会更合适,不是吗?)并使用 XOR 功能进行验证。



这样:

I would use just one event handler for all three checkboxes (btw, you know radio buttons would fit better here, don't you?) and validate using XOR function.

This way:
private void CheckBox_CheckedChanged(object sender, EventArgs e) {
   CheckBox box = (CheckBox)sender;
   if (box != null) {
      if (box.Checked) {
         if (box == toc_Blockleave) {
            toc_Swap.Checked = false;
            toc_Unavailability.Checked = false;
         }
         else if (box == toc_Swap) {
            toc_Blockleave.Checked = false;
            toc_Unavailability.Checked = false;
         }
         else if (box == toc_Unavailability) {
            toc_Blockleave.Checked = false;
            toc_Swap.Checked = false;
         }
      }
   }
   btn_Submit.Enabled = toc_Blockleave.Checked ^ toc_Swap.Checked ^ toc_Unavailability.Checked;
}





希望这会有所帮助。



Hope this helps.


每个CheckBox使用== false



Use == false for each CheckBox

if (rdo_ApplySelectedCodeToRD.Checked == false && rdo_LeaveRDAlone.Checked == false && rdo_SetRDToZR.Checked == false)
            {
                MessageBox.Show("You must choose a Rest Day Option");
                return;
            }


if (toc_Blockleave.Checked ==false && toc_Swap.Checked==false && toc_Unavailability.Checked == false )
{

}

use '&&' operator not'&'


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

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