概述
我正在做这个项目,其中有 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
它们自己但是我真的不确定它是否会是可能的。如果有重构此功能的简单方法,如果有人可以提供帮助,我将不胜感激。