/// <summary>
/// 验证输入框的输入内容
/// </summary>
/// <param name="textbox">输入框控件</param>
/// <param name="length">最大输入长度</param>
/// <param name="controlName">输入框的名称</param>
/// <returns>true验证通过,false验证不通过</returns>
private bool validaing(TextBox textbox, int length, string controlName)
{
bool result = false;
if (textbox.TextLength == 0)
{
errorProvider1.SetError(textbox, "请输入" + controlName + "内容!");
textbox.SelectAll();
textbox.Focus();
}
else if (textbox.TextLength > length)
{
errorProvider1.SetError(textbox, "请输入小于" + length.ToString() + "个字符的" + controlName + "!");
textbox.SelectAll();
textbox.Focus();
}
else {
errorProvider1.SetError(textbox, "");
result = true;
}
return result;
}
用的时候就可以这样/// 验证输入框的输入内容
/// </summary>
/// <param name="textbox">输入框控件</param>
/// <param name="length">最大输入长度</param>
/// <param name="controlName">输入框的名称</param>
/// <returns>true验证通过,false验证不通过</returns>
private bool validaing(TextBox textbox, int length, string controlName)
{
bool result = false;
if (textbox.TextLength == 0)
{
errorProvider1.SetError(textbox, "请输入" + controlName + "内容!");
textbox.SelectAll();
textbox.Focus();
}
else if (textbox.TextLength > length)
{
errorProvider1.SetError(textbox, "请输入小于" + length.ToString() + "个字符的" + controlName + "!");
textbox.SelectAll();
textbox.Focus();
}
else {
errorProvider1.SetError(textbox, "");
result = true;
}
return result;
}
if (validaing(txtManuName, 50, "制造商名称") == false)
{ return; }
if (validaing(txtInStockBatch, 50, "入库批次号") == false)
return;
if (validaing(txtID, 50, "设备ID") == false)
return;
if (validaing(txtRemark, 200, "入库备注") == false)
return;