简单bool验证中的错误 [英] error in simple bool validation

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

问题描述

我已经动态添加了usercontrol所以我想以简单的方式验证它我用它进行验证但是进展不顺利

如何以简单的方式在bool中进行验证

I have added usercontrol dynamically so i want to validate it in simple way i used this for validation but it is not going well
how to have validation in bool in simple way

bool tat;
        public bool val2()
        {

            foreach (Control item in panel1.Controls.OfType<ComboBox>())
            {
                if (item.Text == string.Empty)
                {
                    tat = true;
                }
                else
                {
                    tat = false;
                }
            }
            return tat;

        }

        private void button2_Click(object sender, EventArgs e)
        {

            bool valo = val2();

            if (!valo)
            {
                Form4 fp = new Form4();
                fp.Show();
            }
            else
            {
                MessageBox.Show("error");

            }

        }



我一直点击按钮表格显示,我的代码没有检查错误,怎么做


all the time i click button the form shows up, my code is not examining the error,how to do that

推荐答案

问题是你的验证方法只返回最后的结果 - 如果列表中的最后一个组合框不为空,它将返回false。

尝试中断失败,或返回



顺便说一句:你知道吗,你可以更轻松,更准确地完成测试吗?

The problem is that your validation method returns only the last result - if the final combobox in the list is not empty, it will return false.
Try break on failure, or return

BTW: Did you know you can do the test a lot easier and more accurately?
public bool val2()
{
    foreach (Control item in panel1.Controls.OfType<ComboBox>())
    {
        if (string.IsNullOrWhiteSpace(item.Text))
        {
            return true;
        }
    }
    return false
}





哦, tat 不应该是班级变量 - 如果按照自己的方式进行, tat 应该是方法的本地方法,而不是在方法之外声明。



Oh, and tat should not be a class level variable - if you are going to do it your way, tat should be local to the method, not declared outside it.


方法Val将返回成员变量tat的值,默认情况下为false。

如果panel.Controls不包含任何组合框类型控件,Val将始终返回false或最后设置值



更多关于你正在检查!valo显示你的表格,当valo == true时你显示错误。



在发布此处之前更正您的代码并自行调试...它将节省太多ppls时间。
the Method Val will return the value of the member variable tat which is false by default.
also Val will always return false or last set value if panel.Controls doesnt not contain any comboBox type control

More over you are checking for !valo to show your Form and when valo == true you are displaying Error.

correct your code and debug yourself before posting here... it will save too many ppls time.


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

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