2

我的问题:

ErrorProvider 产生的红色图标闪烁 6 次。我希望能够将其设置为闪烁两次。

我遇到的唯一眨眼属性是BlinkRateand BlinkStyle,它们都不会影响眨眼量。

重现:

  1. 在 Visual Studio 中,在设计模式下,将 TextBox 和 ErrorProvider 拖到新窗体上。
  2. 按原样使用以下内容:

代码:

Public Class Form1
    Private Sub Textbox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        If Not IsNumeric(TextBox1.Text) Then
            ErrorProvider1.SetError(TextBox1, "Numeric input only!")
        Else
            ErrorProvider1.SetError(TextBox1, "")
        End If
    End Sub
End Class
4

1 回答 1

6

这是在Winforms 源代码中硬编码的。

如果你真的想的话,它可以被弄乱。反射可用于访问私有成员。通常有点冒险,但 Winforms 代码已经稳定了很长时间,不会再改变了。我将发布 C# 版本,您可以很容易地将其转换为 VB:

using System.Reflection;
...
    public static void ReportError(ErrorProvider provider, Control control, string text) {
        if (provider.GetError(control) == text) return;
        provider.SetError(control, text);
        // Dig out the "items" hash table to get access to the ControlItem for the control
        var fi = typeof(ErrorProvider).GetField("items", BindingFlags.NonPublic | BindingFlags.Instance);
        var ht = (System.Collections.Hashtable)(fi.GetValue(provider));
        var ci = ht[control];
        // Patch the ControlItem.blinkPhase field to lower it to 2 blinks
        var fi2 = ci.GetType().GetField("blinkPhase", BindingFlags.NonPublic | BindingFlags.Instance);
        if ((int)fi2.GetValue(ci) > 4) fi2.SetValue(ci, 4);
    }

工作正常,风险低。

于 2014-10-25T16:14:44.137 回答