如何以编程方式为所有控件设置 ErrorProvider 图标
我们使用派生的表单类,并为我们的软件提供一个基本表单类。
在派生表单上,我们广泛使用 DataBinding 来处理 BusinessObjects,所有这些都实现 IDataErrorInfo,通过 ErrorProviders 将自定义错误消息抛出到 GUI 的错误输入上。
我现在寻找一种在基本表单类中实现函数的方法,以获取表单上的所有 ErrorProvider-Components 并将表单上每个控件的 IconAlignment 设置为左侧(因为右侧是间距问题)。
欢迎任何提示...
设置图标对齐的代码:
private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
{
errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
foreach (Control subControl in control.Controls)
{
SetErrorProviderIcon(errorProvider, subControl);
}
}
We use derived Form-Classes, with one base form class for our software.
On the derived forms we use DataBinding extensively to deal with our BusinessObjects, all implementing IDataErrorInfo, throwing custom error-messages on false inputs to the GUI with ErrorProviders.
I now search for a way to implement a function in the base-form-class to get all ErrorProvider-Components on a Form and set the IconAlignment for every Control on the Form to left (since right is a spacing-problem).
Any Hints welcome...
Code for Setting the IconAlignment:
private void SetErrorProviderIconAlignment(ErrorProvider errorProvider, Control control)
{
errorProvider.SetIconAlignment(control, ErrorIconAlignment.MiddleLeft);
foreach (Control subControl in control.Controls)
{
SetErrorProviderIcon(errorProvider, subControl);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们使用继承的 ErrorProvider 组件来强制设置/返回 IconAlignment 扩展属性的默认值。
例如,
然后您可以轻松搜索/替换
new ErrorProvider()
并替换为new MyErrorProvider()
。我记不清了,但您可能会发现您可能需要打开表单的设计器,以便让它重新序列化传递到
form.designer.cs
SetIconAlignment 的值> 文件...We used an inherited
ErrorProvider
component instead which forcefully set/returned the default for the IconAlignment extended property.E.g.
Then you could easily do a search/replace for
new ErrorProvider()
and replace withnew MyErrorProvider()
.I cannot recall exactly, but you may find that you may need to open the Form's designer in order to get it to reserialize the value passed into
SetIconAlignment
in theform.designer.cs
files...