使用布尔值管理启用状态的自定义按钮
Custom button with booleans managing enabled state
我想创建一个自定义按钮,仅当多个布尔值为真时才启用该按钮。
因此,每次这些布尔值之一更改其状态时,也会检查其他布尔值的状态,并因此设置按钮的启用状态。
我想布尔值的变化一定是某种火灾事件,但我不知道该怎么做。
这是我的代码(只有一个布尔值,易于阅读且...正确)。链接到布尔值变化的触发事件被触发,但按钮的启用状态没有改变。
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
你们非常接近。这是基本结构:
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
请注意,仅当您希望事件能够从外部处理时才需要这些事件。如果你不想这样,你可以从属性中调用私有方法:
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
我想创建一个自定义按钮,仅当多个布尔值为真时才启用该按钮。
因此,每次这些布尔值之一更改其状态时,也会检查其他布尔值的状态,并因此设置按钮的启用状态。
我想布尔值的变化一定是某种火灾事件,但我不知道该怎么做。
这是我的代码(只有一个布尔值,易于阅读且...正确)。链接到布尔值变化的触发事件被触发,但按钮的启用状态没有改变。
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
你们非常接近。这是基本结构:
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
请注意,仅当您希望事件能够从外部处理时才需要这些事件。如果你不想这样,你可以从属性中调用私有方法:
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