这几天都在写一些类似的东西,就想着偷偷懒,省了不少的东西,
/**/
/// <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)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
bool result = false;
if (textbox.TextLength == 0)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
errorProvider1.SetError(textbox, "请输入" + controlName + "内容!");
textbox.SelectAll();
textbox.Focus();
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
else if (textbox.TextLength > length)
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
errorProvider1.SetError(textbox, "请输入小于" + length.ToString() + "个字符的" + controlName + "!");
textbox.SelectAll();
textbox.Focus();
}
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
else
{
errorProvider1.SetError(textbox, "");
result = true;
}
return result;
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
}
用的时候就可以这样
if
(validaing(txtManuName,
50
,
"
制造商名称
"
)
==
false
)
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{ return; }
if
(validaing(txtInStockBatch,
50
,
"
入库批次号
"
)
==
false
)
return
;
if
(validaing(txtID,
50
,
"
设备ID
"
)
==
false
)
return
;
if
(validaing(txtRemark,
200
,
"
入库备注
"
)
==
false
)
return
;
不知道大家有没有什么更好的办法,交流交流!