0

我试图做的是即使在文本框控件上也有一个自定义,因此当控件上的布尔值设置为 false 时,它​​会触发此事件:

Public Property isError As Boolean = False

    Public Event IsInError As EventHandler

    Private Sub textInError() Handles Me.IsInError
        If isError = False Then
            Me.BackColor = isErrorColor
        End If
    End Sub

我以前从未真正使用过事件处理程序,所以我对它们不太熟悉,所以我很可能在这里走错了路

谢谢

4

1 回答 1

1

是的,你在这个错误的轨道上。听你自己的事件总是一个强烈的迹象,表明你弄错了。您想编写一个属性设置器。像这样:

Public Property IsError() As Boolean
    Get
        Return hasError
    End Get
    Set(ByVal value As Boolean)
        If value == hasError Then Return
        hasError = value
        If hasError Then
            prevBackColor = Me.BackColor
            Me.BackColor = isErrorColor
            '' RaiseEvent IsInError(Me, EventArgs.Empty)  '' If you still need the event
        Else
            Me.BackColor = prevBackColor
        End If
    End Set
End Property

Private hasError As Boolean
Private prevBackColor As Color
于 2012-05-07T19:50:22.003 回答