0

概述

我正在做这个项目,其中有 12 个error providers,所有这些都有自己独特的名称和目的。我知道我可以使用一个,但是我需要确保他们都有自己的用户错误消息。该项目是winform并且我希望在我的保存按钮上评估所有验证,该按钮订阅了 click event.

我的问题

在继续使用保存功能之前,我需要能够评估是否有任何error providers活动。我有以下代码可以工作,但是它相当繁琐且冗长。我还需要向用户显示错误消息,以便他们可以指示哪些字段是无效的。

验证方法

bool IsValidIn()
            {
                foreach (Control c in panel1.Controls)
                {
                    if (c is SpellBox)
                    {
                        //SpellBox txt = (SpellBox)c;
                        if (epForeName.GetError(c).Length > 0 || epSurname.GetError(c).Length > 0 
                                                              || epPostcode.GetError(c).Length > 0
                                                              || epCountry.GetError(c).Length > 0
                                                              || epCounty.GetError(c).Length > 0
                                                              || epHouseName.GetError(c).Length > 0
                                                              || epLocality.GetError(c).Length > 0
                                                              || epStreetName.GetError(c).Length > 0
                                                              || epMobile.GetError(c).Length > 0
                                                              || epLandline.GetError(c).Length > 0
                                                              || epAlternative.GetError(c).Length > 0
                                                              || epEmail.GetError(c).Length > 0)
                        { return false; }
                    }
                }
                return true;}

保存点击事件

 public void btn_SaveDetails_Click(object sender, EventArgs e)
        {
            try
            {

                //txt_ForeName_Validated(this, e);


                if (IsValidIn())
                {

                    _IsValid = true;
                    BtnPressed = "Ok";
                    HouseName = txt_HouseName.Text;
                    HouseNumber = Convert.ToString(nud_HouseNumber.Value);
                    StreetName = txt_StreetName.Text;
                    Locality = txt_Locality.Text;
                    Town = txt_Town.Text;
                    County = txt_County.Text;
                    Country = txt_Country.Text;
                    PostCode = txt_Postcode.Text;
                    Email = txt_Email.Text;
                    Title = cmb_Title.Text;
                    BirthDate = Convert.ToString(dateTimePicker1.Text);
                    ForeName = txt_ForeName.Text;
                    SurName = txt_SurName.Text;
                    PrefContNum = cmb_PrefConNumber.Text;
                    PrefContTime = cmb_PrefConTime.Text;
                    Mobile = txt_Mobile.Text;
                    Landline = txt_LndLine.Text;
                    Alternative = txt_Alt.Text;

                    this.Close();

                }
                else
                {
                    MessageBox.Show("Errors present in form, please review!!!"); //MessageBoxButtons.YesNo) == DialogResult.Yes);
                }
            }

可能的解决方案。

我正在考虑可能将变量存储到某种集合中,很可能是字典,通过将我的变量存储到一个动态变量中,然后遍历Error-providers它们自己但是我真的不确定它是否会是可能的。如果有重构此功能的简单方法,如果有人可以提供帮助,我将不胜感激。

4

2 回答 2

1

这里没有必要使用动态。您可以简单地将ErrorProvider对象存储在List<ErrorProvider>. 用于errorProviders.Any(e => e.GetError(c).Length > 0)确定是否存在任何错误。

于 2017-07-13T14:42:31.803 回答
0

我认为这会给你想要的

bool IsValidIn()
            {
                foreach (Control c in panel1.Controls)
                {
                    if (c is SpellBox)
                    {
                        //SpellBox txt = (SpellBox)c;
                        string errorStr = string.Empty;
                        if (epForeName.GetError(c).Length > 0)
                            errorStr = "epForeName";
                        else if(epSurname.GetError(c).Length > 0)
                            errorStr = "epSurname";
                        .
                        .
                        .
                        else if(epEmail.GetError(c).Length > 0)
                            errorStr = "epEmail";


                        if(errorStr != String.Empty) 
                            return false; 

                    }
                }
                return true;}

// 只返回 errorStr 以获得错误。

于 2017-07-13T14:37:43.627 回答