Home > Mobile >  Custom button with booleans managing enabled state
Custom button with booleans managing enabled state

Time:03-31

I'd like to create a custom button which get enabled only if several booleans is true.

So, each time one of these booleans change its state, the state of the other booleans would be checked too and the enabled state of the button would be set in consequence.

I guess booleans changes have to be some kind of fire events but i don't know how to do it.

Here's my code (with only one boolean, easy to read and...correct). The fire event linked to the boolean change is fired but the Enabled state of the button doesn't change.

Public Class LaunchMassiveImportButton
    Inherits Button
    Public _blnIdGoodToGo As Boolean    
    Private _Enabled As Boolean
    Property Enabled As Boolean
        Get
            Return Me.Enabled
        End Get
        Set(value As Boolean)
            _Enabled = GetStateFromBoolean()
        End Set
    End Property
    Private Function GetStateFromBoolean()
        If blnIdGoodToGo = True Then
            Return True
        Else
            Return False
        End If
    End Function
    Public Property blnIdGoodToGo() As Boolean
        Get
            Return Me._blnIdGoodToGo
        End Get
        Set(ByVal value As Boolean)
            If Me._blnIdGoodToGo <> value Then
                Me._blnIdGoodToGo = value
                Me.OnblnIdGoodToGoChangedChanged(EventArgs.Empty)
            End If
        End Set
    End Property
    Public Event blnIdGoodToGoChangedChanged As EventHandler
    Protected Overridable Sub OnblnIdGoodToGoChangedChanged(ByVal e As EventArgs)
        RaiseEvent blnIdGoodToGoChangedChanged(Me, e)
        Me.Enabled = GetStateFromBoolean()
    End Sub
End Class

CodePudding user response:

You were pretty close. This is the basic structure:

Public Class ButtonEx
    Inherits Button

    Private _flag1 As Boolean
    Private _flag2 As Boolean

    Public Property Flag1 As Boolean
        Get
            Return _flag1
        End Get
        Set
            If _flag1 <> Value Then
                _flag1 = Value
                OnFlag1Changed(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Property Flag2 As Boolean
        Get
            Return _flag2
        End Get
        Set
            If _flag2 <> Value Then
                _flag2 = Value
                OnFlag2Changed(EventArgs.Empty)
            End If
        End Set
    End Property

    Public Event Flag1Changed As EventHandler
    Public Event Flag2Changed As EventHandler

    Protected Overridable Sub OnFlag1Changed(e As EventArgs)
        RaiseEvent Flag1Changed(Me, e)
        SetEnabledState()
    End Sub

    Protected Overridable Sub OnFlag2Changed(e As EventArgs)
        RaiseEvent Flag2Changed(Me, e)
        SetEnabledState()
    End Sub

    Private Sub SetEnabledState()
        Enabled = Flag1 AndAlso Flag2
    End Sub

End Class

Note that you would only need the events if you want them to be able to be handled from the outside. If you don't want that, you can just call the private method from the properties:

Public Class ButtonEx
    Inherits Button

    Private _flag1 As Boolean
    Private _flag2 As Boolean

    Public Property Flag1 As Boolean
        Get
            Return _flag1
        End Get
        Set
            If _flag1 <> Value Then
                _flag1 = Value
                SetEnabledState()
            End If
        End Set
    End Property

    Public Property Flag2 As Boolean
        Get
            Return _flag2
        End Get
        Set
            If _flag2 <> Value Then
                _flag2 = Value
                SetEnabledState()
            End If
        End Set
    End Property

    Private Sub SetEnabledState()
        Enabled = Flag1 AndAlso Flag2
    End Sub

End Class
  • Related