ErrorProvider 作为“警告提供程序”






4.44/5 (6投票s)
使用 ErrorProvider 显示友好的“必填值”警告。
引言
我喜欢为我所开发的应用程序的用户提供友好的用户界面。 一种方法是在添加新记录时“指出”必填字段。
背景
我一直喜欢使用 ErrorProvider
作为一种简单的方式来提醒用户出了问题。 由于与 ErrorProvider
关联的图标和消息可以轻松更改,因此我认为这会是创建“警告提供程序”的好方法。
必备组件
首先,我需要指出我们将访问控件上的 DataBinding 信息。 因此,您需要使用绑定控件才能使其工作。
其次,我们将检查 DataSet
上的约束。 希望阅读本文的任何人都充分利用强类型 DataSet
。 如果您没有,请给自己一个小小的耳光,然后去阅读一些关于强类型 DataSet
的文章。 如果您**不**使用强类型 DataSet
,只要您设置了适当的约束,“警告提供程序”仍然有效。
第三,如果您不想要“警告”的默认错误图标,请将 ImageList
控件添加到您的控件/窗体,并添加您希望使用的 16x16 图标。
第四,创建一个名为“GetDataSet()
”的方法,它将为您提供您的 DataSet
对象。
设置“警告提供程序”
为了本文的目的,我们假设您有一个名为 AppDataSet
的强类型 DataSet
,它包含一个名为 Customer
的 DataTable
,其中包含两个必填字段:Customer_Name
和 Customer_Type
。
在您的窗体/控件中,在声明部分,添加以下代码行
private ErrorProvider warningProvider = new ErrorProvider();
warningProvider.ContainerControl = this ;
现在,你们中的一些人可能在更改错误提供程序的图标时遇到过类似的问题。 如果我在设计时将 ErrorProvider
放在我的控件/窗体上,然后更改了图标,那么我在启动我的应用程序时总是会得到一个 SystemException
。 在未能成功排除故障后,我决定通过代码更改图标,并且我从未遇到过任何问题。
要更改错误提供程序上的图标,请使用以下代码
//assuming the image I want is at position 0
this.warningProvider.Icon =
Icon.FromHandle(((Bitmap)YourImageList.Images[0]).GetHicon());
我还将闪烁样式更改为 NeverBlink
,因为我不想吓到用户。
this.warningProvider.BlinkStyle = ErrorBlinkStyle.NeverBlink;
CheckNewRecordRequiredControls 方法
这是本文的重点。 这是一个递归方法,它将检查所有控件和包含的控件。 它将检查每个控件的databindings,并确定该控件是否绑定到必填字段。
为了本文的目的,我们将只检查 TextBox
es 和 ComboBox
es
/// <summary>
/// Recursively check all controls contained
/// within [ctl] to see if they required a value
/// </summary>
/// <param name="ctl">parent control that contains databound controls</param>
private void CheckNewRecordRequiredControls(Control ctl)
{
foreach (Control control in ctl.Controls)
{
//if the current control contains additional controls, run recursively
if (control.Controls.Count > 0)
{CheckNewRecordRequiredControls(control) ; }
if ((control is TextBox && control.Text == "") ||
(control is ComboBox &&
(control as ComboBox).SelectedIndex == -1))
{
//make sure we have databindings
if (control.DataBindings.Count == 0) { continue ; }
//get bound field name
string boundField =
control.DataBindings[0].BindingMemberInfo.BindingField;
//get the bound table -- if we're bound to a dataview,
//we need to get the table from that
// otherwise just get to the DataTable
string boundTable = string.Empty ;
if (control.DataBindings[0].DataSource is DataView)
{
boundTable = (control.DataBindings[0].DataSource
as DataView).Table.TableName ;
}
else if (control.DataBindings[0].DataSource is DataTable)
{
boundTable = (control.DataBindings[0].DataSource
as DataTable).TableName ;
}
else
{
//not set up to handle bindings to anything
//other than DataView or DataTable
continue ;
}
if (GetDataSet().Tables[boundTable].Columns[boundField].AllowDBNull == false)
{
warningProvider.SetError(control,"Required Field") ;
}
}
}
清除 WarningProvider
一旦设置了 WarningProvider
,您确实需要在适当的时候管理清除它。 我有控件“Leave
”事件的事件处理程序。 我检查控件是否有效,然后清除 WarningProvider
。 如果您只想确保已输入值,那么您可以使用类似于“CheckNewRecordRequiredControls
”的过程,如果控件有一个值,那么像这样清除 WarningProvider
warningProvider.SetError([control],"") ; //the "" will clear the warning icon
我希望您觉得本文有用。