2016-10-25 6 views
0

私は、Production Monitoring Systemというシステムを開発しています。私はCombobox1を別のフォームから90個のボタン名をロードし、次にCombobox2、omコンボボックス2、それに値(MASS PRO、NEW TRIAL、MACHINE ERROR、ETC ...)を持っています。ユーザーがMASS PROを選択した場合、Combobox1から選択されたボタンは、その色をGREENに変えます。ここに私のサンプルコードがあります。私はそれを試したが、何も起こらない。私の問題は、CB2から選択した状態に基づいてボタンの背景色を変更する方法です。コンボボックスを使用して、90のボタンの色を別のフォーム(一度に1つずつ)から変更する方法。

If ComboBox2.SelectedItem = "MASS PRO" Then 
     ComboBox1.SelectedItem.ForeColor = (Color.Green) 
    ElseIf ComboBox2.Text = "NEW TRIAL" Then 
     ComboBox1.SelectedItem.ForeColor = Color.Blue 
    ElseIf ComboBox2.Text = "FIRST MOULDING" Then 
     ComboBox1.SelectedItem.ForeColor = Color.Orange 
    ElseIf ComboBox2.Text = "STOP PRODUCTION" Then 
     ComboBox1.SelectedItem.ForeColor = Color.Red 
    ElseIf ComboBox2.Text = "MASS PRO w/ QN" Then 
     ComboBox1.SelectedItem.ForeColor= Color.Yellow 
    ElseIf ComboBox2.Text = "MACHINE ERROR" Then 
     ComboBox1.SelectedItem.ForeColor= Color.DarkRed 
    End If 

答えて

1

あなたはにButtonの名前をロードすると言っています。 ButtonのNAMEのForeColorを変更する際にどのように使用されますか?名前はButtonではありません。あなたは実際にButtonを取得する必要があります。

多くの選択肢がありますが、Buttonsをすべてリストに追加してComboBoxにバインドし、DisplayMemberを「名前」に設定します。そうすれば、項目は実際にButtonsであり、値はNameと表示されます。

ComboBoxの名前を使用して、ButtonをフォームのControlsコレクションから取得することもできます。

Me.Controls(buttonName).ForeColor = myColor 

別のオプションは、配列にButtonsを入れた後、インデックスで右のいずれかを取得するためにComboBoxSelectedIndexを使用することです。

0

状態に応じていくつかのコントロールの背景色を変更するには、別の状態をあなたの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 

私は、影響を受けるコントロールの単一のカテゴリに値を設定することで、単純なものにしました。

enter image description here

enter image description here

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のビット(あなたのコードは同時により明確になり、よりシンプルになります)。

関連する問題