【发布时间】:2019-02-08 10:00:08
【问题描述】:
我正在设置一个 windowsform 来 ping 我工作中的所有 IP 并相应地更改 button.BackColor。
Ping pingClassxx = new Ping();
PingReply pingxx = pingClassxx.Send("192.168.xx.xx");
if (pingxx.Status == IPStatus.Success)
{
button1.BackColor = Color.Green;
button1.Text = "SQL Server | Online";
}
else
{
button1.BackColor = Color.Red;
button1.Text = "SQL Server | Offline";
}
现在我正在尝试对多台机器执行此操作,所以我是这样的:
private void timer0_Tick(object sender, EventArgs e)
{
timer1.Stop();
int iCount0 = 0;
string[] arr = new string[8];
arr[0] = "192.168.x.xx"; //button0
arr[1] = "192.168.x.yy"; //button1
arr[2] = "192.168.x.zz"; //button2
arr[3] = "192.168.x.xy"; //button3
arr[4] = "192.168.x.xz"; //button4
arr[5] = "192.168.x.yx"; //button5
arr[6] = "192.168.yy.yz"; //button6
arr[7] = "192.168.x.ww"; //button7
for (int I = 0; I < arr.Length ; I++)
{
// Ping
string s = arr[I];
Console.WriteLine(s);
Ping pingClassx = new Ping();
PingReply pingx = pingClassx.Send(arr[I]);
if (pingx.Status == IPStatus.Success)
{
[What do I do here?].BackColor = Color.Green;
}
else
{
[What do I do here?]BackColor = Color.Red;
}
// Loading Bar
iCount2 += 1;
progressBar0.Value = iCount2 * 100 / 8;
label0.Text = iCount2 + "/8";
}
timer1.Start();
}
如何循环播放?
我可以重复代码,但我确信有更好的方法来做到这一点。
【问题讨论】:
-
把你的按钮放在数组中,像这样 Button array[] = {butt1,butt2....} 然后使用这个数组在循环中改变颜色
-
您的表单中是否已经拥有所有这些按钮,或者您需要即时创建它们?这些按钮有什么用处吗,一个执行某些操作的事件处理程序?
-
我已经在表单中拥有了所有这些按钮,这些按钮将在未来使用......现在我只是不明白如何循环我的按钮 btn1、btn2......
-
我期待像 for (i
-
就这么简单,您在第一条评论中就有了解决方案。使用您的 Buttons 引用构建一个数组,然后使用数组索引器访问它们中的每一个。