【发布时间】:2017-05-25 02:39:21
【问题描述】:
我使用 vb.net 2008 构建应用程序。我有一个包含 50 个文本框的表单,其中包含远程设备的 IP 地址,如果 ping 设备良好,则文本框的背景颜色为绿色,否则为红色。我使用 If 函数如下:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If My.Computer.Network.Ping(TextBox1.Text) Then
TextBox1.BackColor = Color.Green
Else
TextBox1.BackColor = Color.Red
End If
If My.Computer.Network.Ping(TextBox2.Text) Then
TextBox2.BackColor = Color.Green
Else
TextBox2.BackColor = Color.Red
End If
.
.’ The if functions of the Textbox3 to the Textbox49
.
If My.Computer.Network.Ping(TextBox50.Text) Then
TextBox50.BackColor = Color.Green
Else
TextBox50.BackColor = Color.Red
End If
End Sub
End Class
对于 50 个文本框,我必须使用 50 个 If 函数,因为这会使代码很长,你能帮我用 For ... Next 循环缩短代码吗? 感谢您的帮助。
【问题讨论】:
-
这看起来像是一个函数的工作。
-
编写您认为应该的循环,如果它不起作用,我们可以帮助您修复它。如果您不知道如何编写
For或For Each循环,请阅读该主题。我们在这里帮助解决特定问题,而不是为您编写代码。如果您还没有尝试编写代码,那么您还没有遇到特定问题。
标签: vb.net