我有一系列validation events
,我想将它们全部分组以最小化代码。
我说这个event
对于大约4个文本字段完全相同。
事件处理程序
private void txt_SurName_Validating(object sender, CancelEventArgs e)
{ // Convert User input to TitleCase
if (useUnkownEntity(txt_SurName.Text))
{
return;
}
if (string.IsNullOrWhiteSpace(txt_SurName.Text))
{
epSurname.Clear(); _IsValid = true;
}
else if (util.IsAllLetters(txt_SurName.Text))
{
epSurname.Clear(); txt_SurName.Text = util.ToTitle(txt_SurName.Text); _IsValid = true;
}
else
{
epSurname.SetError(txt_SurName, "InValid Surname"); _IsValid = false;
}
}
I have tried this however to no luck
this.txt_ForeName.Validating += new System.ComponentModel.CancelEventHandler(this.txt_SurName_Validating);
private void txt_ForeName_Validated(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_ForeName.Text))
{
epForeName.Clear(); _IsValid = true;
}
else if (util.IsAllLetters(txt_ForeName.Text))
{
epForeName.Clear(); txt_ForeName.Text = util.ToTitle(txt_ForeName.Text); _IsValid = true;
}
else
{
epForeName.SetError(txt_ForeName, "InValid Forename, please reType!!"); _IsValid = false;
}
}
/// <summary>
/// Country Validating event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Country_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_Country.Text))
{
epCountry.Clear(); _IsValid = true;
}
else if (util.IsAllLetters(txt_Country.Text))
{
epCountry.Clear(); txt_Country.Text = util.ToTitle(txt_Country.Text); _IsValid = true;
}
else
{
epCountry.SetError(txt_Country, "InValid Country, please reType!!"); _IsValid = false;
}
}
/// <summary>
/// Alternative Vlaiditn event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Alt_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_Alt.Text))
{
epAlternative.Clear(); _IsValid = true;
}
else if (util.numValidation(txt_Alt.Text))
{
epAlternative.Clear(); txt_Alt.Text = util.ToTitle(txt_Alt.Text); _IsValid = true;
}
else
{
epAlternative.SetError(txt_Alt, "InValid Alternative entry!!"); _IsValid = false;
}
}
/// <summary>
/// Landline Validating event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_LndLine_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_LndLine.Text))
{
epLandline.Clear();
_IsValid = true;
}
else if (util.numValidation(txt_LndLine.Text))
{
epLandline.Clear(); txt_LndLine.Text = util.ToTitle(txt_LndLine.Text); _IsValid = true;
}
else
{
epLandline.SetError(txt_LndLine, "InValid LandLine Number, please reType!!"); _IsValid = false;
}
}
/// <summary>
/// PostCode Validating event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Postcode_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_Postcode.Text))
{
epPostcode.Clear(); _IsValid = true;
}
else if (util.IsAllLettersOrDigits(txt_Postcode.Text))
{
epPostcode.Clear(); txt_Postcode.Text = txt_Postcode.Text.ToUpper(); _IsValid = true;
}
else
{
epPostcode.SetError(txt_Postcode, "InValid Postcode, please reType!!"); _IsValid = false;
}
}
/// <summary>
/// Mobile Number Validating event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txt_Mobile_Validating(object sender, CancelEventArgs e)
{
if (string.IsNullOrWhiteSpace(txt_Mobile.Text))
{
epMobile.Clear(); _IsValid = true;
}
else if (util.numValidation(txt_Mobile.Text))
{
epMobile.Clear(); txt_Mobile.Text = util.ToTitle(txt_Mobile.Text); _IsValid = true;
}
else
{
epMobile.SetError(txt_Mobile, "InValid Mobile, please reType!!"); _IsValid = false;
}
}
事件
//
// DOB
//
this.DOB.AutoSize = true;
this.DOB.Location = new System.Drawing.Point(147, 88);
this.DOB.Name = "DOB";
this.DOB.Size = new System.Drawing.Size(30, 13);
this.DOB.TabIndex = 88;
this.DOB.Text = "DOB";
//
// txt_County
//
this.txt_County.Location = new System.Drawing.Point(18, 226);
this.txt_County.Name = "txt_County";
this.txt_County.Size = new System.Drawing.Size(276, 23);
this.txt_County.TabIndex = 7;
this.txt_County.Validating += new System.ComponentModel.CancelEventHandler(this.txt_County_Validating);
this.txt_County.Child = new System.Windows.Controls.TextBox();
//
// txt_Locality
//
this.txt_Locality.Location = new System.Drawing.Point(17, 312);
this.txt_Locality.Name = "txt_Locality";
this.txt_Locality.Size = new System.Drawing.Size(277, 23);
this.txt_Locality.TabIndex = 9;
this.txt_Locality.Validating += new System.ComponentModel.CancelEventHandler(this.txt_Locality_Validating);
this.txt_Locality.Child = new System.Windows.Controls.TextBox();
//
// txt_Country
//
this.txt_Country.Location = new System.Drawing.Point(17, 268);
this.txt_Country.Name = "txt_Country";
this.txt_Country.Size = new System.Drawing.Size(277, 21);
this.txt_Country.TabIndex = 8;
this.txt_Country.Validating += new System.ComponentModel.CancelEventHandler(this.txt_Country_Validating);
this.txt_Country.Child = new System.Windows.Controls.TextBox();
//
// txt_HouseName
//
this.txt_HouseName.Location = new System.Drawing.Point(18, 352);
this.txt_HouseName.Name = "txt_HouseName";
this.txt_HouseName.Size = new System.Drawing.Size(276, 21);
this.txt_HouseName.TabIndex = 10;
this.txt_HouseName.Validating += new System.ComponentModel.CancelEventHandler(this.txt_HouseName_Validating);
this.txt_HouseName.Child = new System.Windows.Controls.TextBox();
//
// txt_StreetName
//
this.txt_StreetName.Location = new System.Drawing.Point(17, 393);
this.txt_StreetName.Name = "txt_StreetName";
this.txt_StreetName.Size = new System.Drawing.Size(277, 23);
this.txt_StreetName.TabIndex = 11;
this.txt_StreetName.Validating += new System.ComponentModel.CancelEventHandler(this.txt_StreetName_Validating);
this.txt_StreetName.Child = new System.Windows.Controls.TextBox();
//
// txt_Town
//
this.txt_Town.BackColor = System.Drawing.SystemColors.Info;
this.txt_Town.BackColorTransparent = true;
this.txt_Town.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.txt_Town.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.txt_Town.Location = new System.Drawing.Point(17, 186);
this.txt_Town.Name = "txt_Town";
this.txt_Town.Size = new System.Drawing.Size(277, 20);
this.txt_Town.TabIndex = 6;
this.txt_Town.Validating += new System.ComponentModel.CancelEventHandler(this.txt_Town_Validating);
this.txt_Town.Child = new System.Windows.Controls.TextBox();
//
// txt_ForeName
//
this.txt_ForeName.BackColor = System.Drawing.Color.Honeydew;
this.txt_ForeName.BackColorTransparent = true;
this.txt_ForeName.Font = new System.Drawing.Font("Microsoft YaHei UI", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txt_ForeName.ForeColor = System.Drawing.SystemColors.Info;
this.txt_ForeName.Location = new System.Drawing.Point(17, 23);
this.txt_ForeName.Name = "txt_ForeName";
this.txt_ForeName.Size = new System.Drawing.Size(277, 21);
this.txt_ForeName.TabIndex = 0;
this.txt_ForeName.Validated += new System.EventHandler(this.txt_ForeName_Validated);
this.txt_ForeName.Child = new System.Windows.Controls.TextBox();
//
// txt_SurName
//
this.txt_SurName.Location = new System.Drawing.Point(18, 64);
this.txt_SurName.Name = "txt_SurName";
this.txt_SurName.Size = new System.Drawing.Size(276, 21);
this.txt_SurName.TabIndex = 1;
this.txt_SurName.Validating += new System.ComponentModel.CancelEventHandler(this.txt_SurName_Validating);
this.txt_SurName.Child = new System.Windows.Controls.TextBox();
//
任何想法都会非常感激