Houlahan
Houlahan

Reputation: 783

Control With Custom Event

What im trying to do is have a custom even on a text box control so when the Boolean on the control is set to false it fires off this event:

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

ive never really used event handlers before so im not very familiar with them so i could well be on the wrong path here

Thanks

Upvotes: 0

Views: 85

Answers (1)

Hans Passant
Hans Passant

Reputation: 941465

Yes, you are on the wrong track with this. Listening to your own events is always a strong indication that you are getting it wrong. You want to write a property setter instead. Like this:

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

Upvotes: 1

Related Questions