WinForms中的单选按钮验证

时间:2016-04-25 03:40:07

标签: c# winforms

所以我在不同的组框中有多个不同的单选按钮。在用户能够“保存”他们的表单之前,需要填写所有字段。所以我试图确保所有单选按钮都已填写。目前我正在尝试使用以下代码:

 if (!(this.RoundTrip.Checked || this.OneWay.Checked))
            {
                MessageBox.Show("Select an option for Trip Type");


                if (!(this.NorthRad.Checked || this.ExpressRad.Checked || this.ExpressRad.Checked))
                {
                    MessageBox.Show("Select an Option for Route Type");

                }
                if (!(this.YesNeeded.Checked || this.NotNeeded.Checked))
                {
                    MessageBox.Show("Select an option for accessibility");

                }
                if (this.AdultNum.Value == 0 && this.SeniorNum.Value == 0 && this.ChildNum.Value == 0)
                {
                    MessageBox.Show("Select at least one ticket");

                }
                return;
            }

使用此代码不允许我单击“保存”按钮,但不会显示任何消息框。即使在我填写无线电盒后,我也无法点击保存按钮。任何帮助,将不胜感激。

3 个答案:

答案 0 :(得分:0)

如果您确认没有选择,那么您需要使用&&不是||。

如果未选中任何选项,则需要确认未选择任何单选按钮。使用OR,如果第一个逻辑检查满足条件,那么它将不检查第二个。同样在您的情况下,假设没有其他单选按钮显示N / A或其他内容,您的内部语句将始终为True,因为如果在其位置检查了另一个单选按钮,则不会检查至少一个单选按钮。

答案 1 :(得分:0)

我怀疑它是导致问题的顶级if条件,其他条件无法评估,因为您的测试中顶级条件可能为true

可能你需要的是......

if (!(this.RoundTrip.Checked || this.OneWay.Checked))
{
    MessageBox.Show("Select an option for Trip Type");
}
else if (!(this.NorthRad.Checked || this.ExpressRad.Checked || this.ExpressRad.Checked))
{
    MessageBox.Show("Select an Option for Route Type");

}
else if (!(this.YesNeeded.Checked || this.NotNeeded.Checked))
{
    MessageBox.Show("Select an option for accessibility");

}
else if (this.AdultNum.Value == 0 && this.SeniorNum.Value == 0 && this.ChildNum.Value == 0)
{
    MessageBox.Show("Select at least one ticket");
}
return;

另一方面,由于您有groupbox进行分组,因此您可以使用简单的Linq来评估某个群组。

var group1Validation = GroupBox1.Controls
                          .OfType<RadioButton>()
                          .Any(r=>r.Checked); 


var group2Validation = GroupBox2.Controls
                          .OfType<RadioButton>()
                          .Any(r=>r.Checked); 


if(!group1)
{
    MessageBox.Show("Select an option for Trip Type");
    ...
}

答案 2 :(得分:0)

你的逻辑似乎是正确的,但它总会在最后一行返回。 也许你需要的是这样的东西;

private void btnSave_Clicked()
    {
        if (!IsValidDataEntered()) return;

        Save();
    }

    private bool IsValidDataEntered()
    {
        if (!(this.RoundTrip.Checked || this.OneWay.Checked))
            MessageBox.Show("Select an option for Trip Type");

        else if (!(this.NorthRad.Checked || this.ExpressRad.Checked || this.ExpressRad.Checked))
            MessageBox.Show("Select an Option for Route Type");

        else if (!(this.YesNeeded.Checked || this.NotNeeded.Checked))
            MessageBox.Show("Select an option for accessibility");

        else if (this.AdultNum.Value == 0 && this.SeniorNum.Value == 0 &&
                 this.ChildNum.Value == 0)
            MessageBox.Show("Select at least one ticket");

        else
            return true;

        return false;
    }