替换对象名称

卡特鲁斯

我正在尝试根据ping结果使对象更改颜色。我要ping的每个设备都有一个对象,这些对象的名称与计算机相同。因此,我试图用一个变量替换对象名称,该变量可以使用For语句传递throw。

Private Sub PING_Click()
Dim strCommand As String
Dim strPing As String
Dim myIP() As String
ReDim myIP(0 To 3) As String

myIP(0) = "computer1"
myIP(1) = "computer2"
myIP(2) = "computer3"
myIP(3) = "computer4"



For i = 0 To 3
MsgBox myIP(i)


strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 -w 500    " & myIP(i) & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
strPing = fShellRun(strCommand)

objcol = myIP(i) & ".ForegroundColor"

If strPing = "" Then
    objcol = 255
Else
    objcol = 65280

End If

Next i

End

End Sub

我的问题是

    objcol = myIP(i) & ".ForegroundColor"

If strPing = "" Then
    objcol = 255
Else
    objcol = 65280

由于某些原因,它不会更改对象的颜色。这样做很好,如果我硬编码,例如变量

    objcol = myIP(i) & ".ForegroundColor"

If strPing = "" Then
    computer1.ForegroundColor = 255
Else
    computer1.ForegroundColor = 65280 

任何帮助将非常感激。

谢谢

爸爸爸爸

您在混淆这一行:

computer1.foregroundcolor 

其中“ Computer1”表示一个对象,可以使用以下方法更改其前景色:

objcol

这是一个字符串,其值为“ computer1.foregroundcolor”。

为此,您需要创建一个对象数组,如下所示:

Dim myIP() As Object
ReDim myIP(0 To 3) As Object

myIP(0) = computer1
myIP(1) = computer2
myIP(2) = computer3
myIP(3) = computer4

For i = 0 To 3
MsgBox myIP(i)


strCommand = "%ComSpec% /C %SystemRoot%\system32\ping.exe -n 1 -w 500    " &    myIP(i).name & " | " & "%SystemRoot%\system32\find.exe /i " & Chr(34) & "TTL=" & Chr(34)
strPing = fShellRun(strCommand)



    If strPing = "" Then
        myIP(i).foregroundcolor = 255
    Else
        myIP(i).foregroundcolor = 65280

    End If

Next i

End

End Sub

这假定您拥有computer1,computer2作为要放入阵列的对象,并且这些对象具有“ .name”属性,其中包含名称“ computer1”,“ computer2”等。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章