2017-07-13 43 views
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;} 

保存Click事件

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迭代本身不過我真的不確定的可能性是否會有可能。如果有一個簡單的重構這個函數的方法,我會非常感激,如果有人可以幫助。

回答

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用於獲取錯誤的東西。

+0

謝謝但是我得到'error = epEmail'的語法錯誤不能將類型的錯誤提供程序轉換爲字符串。 – whatdoyouNeedFromMe

+0

對不起。答案編輯@whatdoyouNeedFromMe –

+0

對不起,但你的答案只檢查電子郵件,姓氏和姓氏。如果我實施這個evry錯誤prov將會比我目前所做的更長 – whatdoyouNeedFromMe

1

這裏沒有必要使用動態。您可以簡單地將您的ErrorProvider對象存儲在List<ErrorProvider>中。使用errorProviders.Any(e => e.GetError(c).Length > 0)來確定是否存在任何錯誤。

+0

乾杯你能給我一個例子,因爲我的任何錯誤都會得到。我創建它像'List errorProviders =新列表()'我填充我的列表如下errorProviders.Add(epSurname)'然而我不能得到它的工作你能夠提供一個例子如何你意思是 ? – whatdoyouNeedFromMe

+0

@whatdoyouNeedFromMe - 您需要確保您的各種ErrorProvider派生自相同的ErrorProvider類(即:epForeName,epSurname,epPostcode等)。在上面的聲明的一個例子可能是'var errorProviders = new List (){epForeName,epSurname,epPostcode,...};'然後當你試圖單獨檢查是否有錯誤 - 而不是'if(epForeName.GetError(c).Length> 0 || ...'你可以使用'errorProviders.Any(e => e.GetError(c).Length> 0'來替換多行。「 – hsoesanto

+0

@whatdoyouNeedFromMe我以爲你之前沒有填寫清單,你得到了什麼錯誤? – hsoesanto