在Visual Basic中使用不同形式的组合框更改背景颜色

时间:2018-03-14 16:03:28

标签: vb.net

公共类Form4     Private Sub Button1_Click(sender As Object,e As EventArgs)处理Button1.Click         On Error GoTo SaveErr         MachineHistoryBindingSource.EndEdit()         Machine_HistoryTableAdapter.Update(HistoryDataSet.Machine_History)         MessageBox.Show(“OK完成”) SaveErr:         退出子         Dim tempcolor4 As String         Dim tempcolor1 As String

    tempcolor4 = ComboBox4.Text
    tempcolor1 = ComboBox1.Text

    If tempcolor4.Equals("Running") And tempcolor1.Equals("1") Then
        Form9.PictureBox15.BackColor = Color.Green
    ElseIf tempcolor4.Equals("Stop") And tempcolor1.Equals("1") Then
        Form9.PictureBox15.BackColor = Color.Red
    ElseIf tempcolor4.Equals("Setup") And tempcolor1.Equals("1") Then
        Form9.PictureBox15.BackColor = Color.Orange
    ElseIf tempcolor4.Equals("For Setup") And tempcolor1.Equals("1") Then
        Form9.PictureBox15.BackColor = Color.Yellow
    ElseIf tempcolor4.Equals("Idle") And tempcolor1.Equals("1") Then
        Form9.PictureBox15.BackColor = Color.Cyan
    End If

End Sub Private Sub Form4_Load(sender as Object,e As EventArgs)处理MyBase.Load         'TODO:这行代码将数据加载到'HistoryDataSet.Machine_History'表中。您可以根据需要移动或删除它。         Me.Machine_HistoryTableAdapter.Fill(Me.HistoryDataSet.Machine_History)         'TODO:这行代码将数据加载到'HistoryDataSet.Machine_History'表中。您可以根据需要移动或删除它。         Me.Machine_HistoryTableAdapter.Fill(Me.HistoryDataSet.Machine_History)         'TODO:这行代码将数据加载到'HistoryDataSet.Machine_History'表中。您可以根据需要移动或删除它。         Me.Machine_HistoryTableAdapter.Fill(Me.HistoryDataSet.Machine_History)

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    MachineHistoryBindingSource.MovePrevious()
End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    MachineHistoryBindingSource.AddNew()
End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
    MachineHistoryBindingSource.MoveNext()
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    MachineHistoryBindingSource.RemoveCurrent()
End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
    Me.Close()
End Sub

结束班

1 个答案:

答案 0 :(得分:0)

行。那么,你在button.click事件中的代码工作正常。我假设您希望在选择“正在运行”,“已停止”或“设置”时立即更改颜色

不应将代码放在Button1.Click事件处理程序中,而应将其放在ComboBox1.SelectedIndexChanged事件处理程序中。像这样......

它使用Select Case而不是If..Then来减少代码。我还添加了With..End With语句,因此无需输入PictureBox2

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
    Select Case ComboBox1.SelectedItem.ToString
        Case "Running"
            PictureBox2.BackColor = Color.Green
            Form2.PictureBox2.BackColor = Color.Green
        Case "Stop"
            PictureBox2.BackColor = Color.Red
            Form2.PictureBox2.BackColor = Color.Red
        Case "Setup"
            PictureBox2.BackColor = Color.Orange
            Form2.PictureBox2.BackColor = Color.Orange

    End Select

End Sub