C#WinForm验证复选框和错误,如果至少一个未选择 [英] C# WinForm Validate Checkbox and Error if at least one is unselected

查看:419
本文介绍了C#WinForm验证复选框和错误,如果至少一个未选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎对验证我的用户控制有问题。



帮助将是真棒:)



如果我选中了至少一个复选框,我将通过循环处理所有复选框。



如果我没有选中复选框,表单将生成一个错误。 p>

如果只有一个复选框之前的foreach将是一个更容易的解决方案对我来说,但我有很多复选框在这个用户控件,不想列出他们所有.. ..



我认为有一些逻辑问题,甚至选择一个复选框,我仍然得到我的警告messagebox ...和我警告messagebox循环,导致它从来没有close ...

  private void ValidateButton_Click(object sender,EventArgs e)
{
$ b b foreach(var control in this.Controls)
{
if(控件是CheckBox)
{
if((CheckBox)控件).Checked)
{
// CODE GOES HERE to
//进程在后台检入
//然后
validatebutton.hide();
nextbutton.show();
}
}
else
{
DialogResult uncheckederror = MessageBox.Show(您必须至少选择一个复选框,
验证错误! ,MessageBoxButtons.OK,MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
}
}

}

解决方案

检查以下内容:

  private void ValidateButton_Click EventArgs e)
{

Boolean checkboxFlag = false;
foreach(var control in this.Controls)
{
if(control is CheckBox)
{
if(((CheckBox)control).checked)
{
checkboxFlag = true;
validatebutton.hide()
nextbutton.show();

}
}

}
if(!checkboxFlag)
{
DialogResult uncheckederror = MessageBox.Show必须至少选择一个复选框,
Validation Error!,MessageBoxButtons.OK,MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
}


I seem to have a problem with validating my user control.

Help would be awesome :)

If I have at least one check box checked, I will process through a loop all checkboxes.

If I have no checkboxes checked, the form generates an error.

An if with just one checkbox before the foreach would be an easier solution for me but I have quite a lot of checkboxes on this user control and dont want to list them all....

Also I think there is some logic problem, cos even select a checkbox I still get my warning messagebox... and I the warning messagebox loops causing it never to close...

  private void ValidateButton_Click(object sender, EventArgs e)
        {

            foreach (var control in this.Controls)
            {
                if (control is CheckBox)
                {
                    if (((CheckBox)control).Checked)
                    {
                           //CODE GOES HERE to
                           //Process checked in background
                           //and then
                           validatebutton.hide();
                           nextbutton.show();
                    }  
                }
                else
                {
                    DialogResult uncheckederror = MessageBox.Show("You must select at least one checkbox",
                        "Validation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                }
            }

}

解决方案

Check the following:

private void ValidateButton_Click(object sender, EventArgs e)
    {

        Boolean checkboxFlag = false;
        foreach (var control in this.Controls)
        {
            if (control is CheckBox)
            {
                if (((CheckBox) control).Checked)
                {
                    checkboxFlag = true;
                    validatebutton.hide();
                    nextbutton.show();

                }
            }

        }
    if(!checkboxFlag)
        {
            DialogResult uncheckederror = MessageBox.Show("You must select at least one checkbox",
                "Validation Error!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation,
                MessageBoxDefaultButton.Button1);
        }
    }

这篇关于C#WinForm验证复选框和错误,如果至少一个未选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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