首页 文章

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

提问于
浏览
-2

Public Class 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 Complete”)SaveErr:Exit Sub 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)Handles 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

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

    它应该放在 ComboBox1.SelectedIndexChanged 事件处理程序中,而不是在 Button1.Click 事件处理程序中包含代码 . 像这样 ..

    它使用 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
    

相关问题