我正在构建一个VB.Net应用程序,以实时检查生产区域中的机器状态。我想在监视器中显示区域布局,如果机器状态为1,则将其设置为绿色,如果是2则为红色,如果是其他设置为橙色。我有以下代码,但它不起作用,因为它说操作员&没有为类型Control定义,我用它来声明我的标签数组。有人能告诉我,如果我做错了吗? (我是VB.Net的初学者)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim labels() As Control = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10, Label11, Label12, Label13, Label14, Label15, Label16, Label17, Label18, Label19, Label20, Label21, Label22,
Label23, Label24, Label25, Label26, Label27, Label28, Label29, Label30, Label31, Label32, Label33, Label34, Label35, Label36, Label37, Label38, Label39, Label40, Label41, Label42, Label43, Label44, Label45, Label46,
Label47, Label48, Label49, Label50, Label51, Label52, Label53, Label54, Label55, Label56, Label57, Label58, Label59, Label60, Label61, Label62, Label63, Label64, Label65, Label66, Label67, Label68, Label69, Label70,
Label71, Label72}
Dim estado As Integer
Try
con.Open()
For i = 0 To 71
Console.WriteLine(labels(i))
Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i) & ""
Dim myCommand As New MySqlCommand()
myCommand.Connection = con
myCommand.CommandText = sqlquery
Dim objReader As MySqlDataReader = myCommand.ExecuteReader
If objReader.Read = True Then
estado = objReader("IDEstado")
If estado = 1 Then
labels(i).BackColor = System.Drawing.Color.Green
ElseIf estado = 2 Then
labels(i).BackColor = System.Drawing.Color.Red
Else
labels(i).BackColor = System.Drawing.Color.DarkOrange
End If
End If
objReader.Close()
Next
con.Close()
Finally
End Try
End Sub
答案 0 :(得分:1)
你需要使用控件的Text
属性,同时将其连接起来,就像下面你发布的代码一样,你实际上是在试图将字符串与控件连接起来,所以错误。
Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i).Text & ""
答案 1 :(得分:1)
您正在尝试使用sqlquery中的字符串连接Label控件,您需要从标签中获取文本,请参阅下文;
改变这个:
Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i) & ""
到此:
Dim sqlquery = "select IDEstado from Maquinas where IDMaquina = " & labels(i).Text & ""