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



Public Class Form4 私有子Button1_Click(发件人作为对象,e 作为事件参数)句柄按钮1.单击 出错时转到保存错误 MachineHistoryBindingSource.EndEdit() Machine_HistoryTableAdapter.更新(HistoryDataSet.Machine_History) MessageBox.Show("OK Complete") 保存错误: 退出子 暗淡温度颜色4 作为字符串 将温度颜色1 调暗为字符串

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

结束子 私有子Form4_Load(sender as object,e as eventArgs)处理MyBase.Load "TODO:这行代码将数据加载到"HistoryDataSet.Machine_History"表中。您可以根据需要移动或删除它。 Me.Machine_HistoryTableAdapter.填充(Me.HistoryDataSet.Machine_History) "TODO:这行代码将数据加载到"HistoryDataSet.Machine_History"表中。您可以根据需要移动或删除它。 Me.Machine_HistoryTableAdapter.填充(Me.HistoryDataSet.Machine_History) "TODO:这行代码将数据加载到"HistoryDataSet.Machine_History"表中。您可以根据需要移动或删除它。 Me.Machine_HistoryTableAdapter.填充(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

结束类

好的。好吧,您在 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

最新更新