使用自定义事件控制

时间:2012-05-07 19:40:09

标签: vb.net events

我试图做的是甚至在文本框控件上有自定义,所以当控件上的布尔值设置为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

我从来没有真正使用过事件处理程序,所以我不太熟悉它们所以我很可能在这里错误的路径

由于

1 个答案:

答案 0 :(得分:1)

是的,你跟这个走错了路。聆听自己的事件总是强烈表明您错了。您想要编写属性setter。像这样:

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