c# - 在运行时定位 Windows 窗体上的 ErrorProvider

标签 c# .net winforms

我正在制作一个扩展方法库以在 Windows 窗体应用程序中使用。我打算创建的方法之一将使在输入控件上设置错误状态更容易,例如

public static void SetError(this System.Windows.Forms.TextBox textBox, string errorMessage)
{
    if (string.IsNullOrEmpty(errorMessage))
    {
        //reset control state
        textBox.BackColor = System.Drawing.SystemColors.WindowText;
    }
    else
    {
        //set background colour to a nice shade of red
        textBox.BackColor = System.Drawing.Color.MistyRose;
    }

    //try to locate an ErrorProvider on the control's containing form.
    var errorProvider = LocateErrorProvider(textBox);

    if (errorProvider != null)
    {
        //set error message on error provider (or clear it)
        errorProvider.SetError(textBox, errorMessage);
    }
}

我正在尝试找出 LocateErrorProvider 方法。我想做的是检查我的表单上是否存在 ErrorProvider,然后仅在存在时才使用它。

ErrorProvider 是一个 Component 而不是 Control,所以我无法通过 form.Controls 属性访问它。我曾尝试将父表单转换为各种对象,但无济于事。

更新:我已经使用以下代码通过反射成功访问了 ErrorProvider:

private static System.Windows.Forms.ErrorProvider GetErrorProvider(System.Windows.Forms.Control control)
{
    //get the containing form of the control
    var form = control.GetContainerControl();

    //use reflection to get to "components" field
    var componentField = form.GetType().GetField("components", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

    if (componentField != null)
    {
        //get the component collection from field
        var components = componentField.GetValue(form);

        //locate the ErrorProvider within the collection
        return (components as System.ComponentModel.IContainer).Components.OfType<System.Windows.Forms.ErrorProvider>().FirstOrDefault();
    }
    else
    {
        return null;
    }
}

就个人而言,我不太喜欢使用硬编码的字段名称来访问该字段。但在这种情况下,它似乎工作得很好。有人有更好的方法来达到相同的结果吗?

最佳答案

到目前为止,这似乎解决了我的问题:

private static System.Windows.Forms.ErrorProvider GetErrorProvider(System.Windows.Forms.Control control)
{
    //get the containing form of the control
    var form = control.GetContainerControl();

    //use reflection to get to "components" field
    var componentField = form.GetType().GetField("components", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);

    if (componentField != null)
    {
        //get the component collection from field
        var components = componentField.GetValue(form);

        //locate the ErrorProvider within the collection
        return (components as System.ComponentModel.IContainer).Components.OfType<System.Windows.Forms.ErrorProvider>().FirstOrDefault();
    }
    else
    {
        return null;
    }
}

感谢 Hans & Cody 的绝妙想法。

关于c# - 在运行时定位 Windows 窗体上的 ErrorProvider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8817359/

相关文章:

c# - 您将 Form1 重命名为什么?

c# - Clickonce 应用程序看不到 AppSettings

c# - 是否可以为打开/关闭表单创建自定义方法?

c# - 如何在 Windows 7 上删除 Java 程序的标题栏和任务栏图标?

c# - namespace 错误中不存在类型资源

.net - OPC HRESULT E_FAIL 当连接到远程服务器时

c# - 仅移动鼠标时内存消耗不断增加

c# - Windows 窗体应用程序中的左菜单选项卡控件我怎样才能得到它?它在当前控件中可用吗?

c# - 当其他用户的命令完成时,保留用户命令 ASP.Net

c# - 如何以编程方式编辑 .png?