状態に応じていくつかのコントロールの背景色を変更するには、別の状態をあなたのUIをたどるを実現することができます。ここでは
のみ1色でシンプルな例ですが、あなたはそれを拡張したいと思うでしょう。
Public MustInherit Class State
MustOverride ReadOnly Property Color As Color
MustOverride ReadOnly Property Description As String
End Class
Public NotInheritable Class ErrorState
Inherits State
Public Overrides ReadOnly Property Color As Color
Get
Return Color.Red
End Get
End Property
Public Overrides ReadOnly Property Description As String
Get
Return "Error"
End Get
End Property
End Class
Public NotInheritable Class NormalState
Inherits State
Public Overrides ReadOnly Property Color As Color
Get
Return Color.Green
End Get
End Property
Public Overrides ReadOnly Property Description As String
Get
Return "Normal"
End Get
End Property
End Class
使用例:
Public Class Form1
Private _myControls As List(Of Control)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' populate available states
Dim list = New List(Of State)()
list.Add(New NormalState)
list.Add(New ErrorState)
ComboBox1.DisplayMember = "Description"
ComboBox1.DataSource = list
' populate controls affected by current state
_myControls = New List(Of Control)
_myControls.Add(Button1)
_myControls.Add(Button2)
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
' guard
If _myControls Is Nothing
Return
End If
' update controls back color according selected state
Dim comboBox = CType(sender, ComboBox)
Dim state = CType(comboBox.SelectedItem, State)
For Each control As Control In _myControls
control.BackColor = state.Color
Next
' etc ...
End Sub
End Class
私は、影響を受けるコントロールの単一のカテゴリに値を設定することで、単純なものにしました。
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' populate available states
Dim list = New List(Of State)()
list.Add(New NormalState)
list.Add(New ErrorState)
ComboBox1.DisplayMember = "Description"
ComboBox1.DataSource = list
' tag controls
Button1.Tag = StateTag.Important
Button2.Tag = StateTag.Useless
ComboBox1.SelectedIndex = -1
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
' update controls back color according selected state and their tag
Dim comboBox = CType(sender, ComboBox)
If comboBox.SelectedIndex = -1 Then
Return
End If
Dim state = CType(comboBox.SelectedItem, State)
For Each control As Control In Controls
Dim o = control.Tag
If o Is Nothing Then Continue For
Dim defined = [Enum].IsDefined(GetType(StateTag), o)
If Not defined Then Continue For
Dim stateTag = [Enum].Parse(GetType(StateTag), o)
Dim color = state.GetStateTagColor(stateTag)
control.BackColor = color
Next
End Sub
End Class
Public Enum StateTag
Important
Useless
End Enum
Public MustInherit Class State
MustOverride ReadOnly Property Description As String
Public MustOverride Function GetStateTagColor(ByVal tag As StateTag) As Color
End Class
Public NotInheritable Class ErrorState
Inherits State
Protected ReadOnly _dictionary As Dictionary(Of StateTag, Color)
Sub New()
_dictionary = New Dictionary(Of StateTag, Color)()
_dictionary.Add(StateTag.Important, Color.Red)
_dictionary.Add(StateTag.Useless, Color.Green)
End Sub
Public Overrides ReadOnly Property Description As String
Get
Return "Error"
End Get
End Property
Public Overrides Function GetStateTagColor(ByVal tag As StateTag) As Color
Return _dictionary.Item(tag)
End Function
End Class
Public NotInheritable Class NormalState
Inherits State
Protected ReadOnly _dictionary As Dictionary(Of StateTag, Color)
Sub New()
_dictionary = New Dictionary(Of StateTag, Color)()
_dictionary.Add(StateTag.Important, Color.Cyan)
_dictionary.Add(StateTag.Useless, Color.Magenta)
End Sub
Public Overrides ReadOnly Property Description As String
Get
Return "Normal"
End Get
End Property
Public Overrides Function GetStateTagColor(ByVal tag As StateTag) As Color
Return _dictionary.Item(tag)
End Function
End Class
良いことはあなたということです:
別の方法としては、Control.Tag
プロパティを使用して、グループ関連のコントロールに列挙型を使用し、それらを色付けすることができますエラーが発生しやすいstring
をもう使用する必要はありません。 OOPのビット(あなたのコードは同時により明確になり、よりシンプルになります)。