C# 在运行时更改 TextBox BackColor
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9129419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change TextBox BackColor in run time
提问by bakar
I've 4 textboxes and 1 button. When the button is pressed, it pings the 4 ip addresses, then changes the textboxes color according to ping status.
我有 4 个文本框和 1 个按钮。当按下按钮时,它会 ping 4 个 ip 地址,然后根据 ping 状态更改文本框的颜色。
What I want to do is, when button is pressed all textboxes backcolor changes to white before the pings start.
我想要做的是,当按下按钮时,所有文本框的背景色都会在 ping 开始之前变为白色。
I wrote the following code, but it didn't work.
我写了下面的代码,但是没有用。
My Codes:
我的代码:
public void Clear1()
{
txtHKB1.BackColor = Color.Yellow;
txtHKB2.BackColor = Color.Yellow;
txtHKB3.BackColor = Color.Yellow;
txtHKB4.BackColor = Color.Yellow;
}
public void Clear2()
{
txtHKB1.Text = "";
txtHKB2.Text = "";
txtHKB3.Text = "";
txtHKB4.Text = "";
}
private void btnConnect_Click(object sender, EventArgs e)
{
//b.Baglan("192.168.20.50","9050");
}
private void btnSistemIzle_Click(object sender, EventArgs e)
{
Thread th1 = new Thread(new ThreadStart(Clear1));
Thread th2 = new Thread(new ThreadStart(Clear2));
th1.Start();
th2.Start();
SistemIzle("192.168.20.60");
SistemIzle("192.168.20.80");
SistemIzle("192.168.20.100");
SistemIzle("192.168.20.120");
counter2++;
}
public void SystemAnalyse(string ip)
{
try
{
IPAddress ipAddress = Dns.GetHostEntry(ip).AddressList[0];
//for (int i = 0; i < 3; i++)
//{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply pingReply = ping.Send(ipAddress);
counter++;
//MessageBox.Show(pingReply.Buffer.Count().ToString() + pingReply.RoundtripTime.ToString()
// + pingReply.Options.Ttl.ToString() + pingReply.Status.ToString());
//System.Threading.Thread.Sleep(100);
//}
}
catch
{
//MessageBox.Show("Ba?ar?s?z Giri?im!");
fail++;
}
if (counter % 4 == 1 && fail == 0)
{
txtHKB1.BackColor = Color.Green;
txtHKB1.Text = " Yayinda";
}
if (counter % 4 == 1 && fail == 1)
{
fail = 0;
txtHKB1.BackColor = Color.Red;
txtHKB1.Text = " Kapal?";
}
if (counter % 4 == 2 && fail == 0)
{
txtHKB2.BackColor = Color.Green;
txtHKB2.Text = " Yayinda";
}
if (counter % 4 == 2 && fail == 1)
{
fail = 0;
txtHKB2.BackColor = Color.Red;
txtHKB2.Text = " Kapal?";
}
if (counter % 4 == 3 && fail == 0)
{
txtHKB3.BackColor = Color.Green;
txtHKB3.Text = " Yayinda";
}
if (counter % 4 == 3 && fail == 1)
{
fail = 0;
txtHKB3.BackColor = Color.Red;
txtHKB3.Text = " Kapal?";
}
if (counter % 4 == 0 && fail == 0)
{
txtHKB4.BackColor = Color.Green;
txtHKB4.Text = " Yayinda";
}
if (counter % 4 == 0 && fail == 1)
{
fail = 0;
txtHKB4.BackColor = Color.Red;
txtHKB4.Text = " Kapal?";
}
}
What I'm doing wrong? My best regards...
我做错了什么?我最诚挚的问候...
采纳答案by Tudor
This code does not make much sense. You are spawning two threads just to change the color of controls that are owned by a different thread? This is wrong for many reasons:
这段代码没有多大意义。您生成两个线程只是为了更改不同线程拥有的控件的颜色?这是错误的,原因有很多:
- Why would you need to change the color in parallel?
- You cannot do it like this anyway, because only the UI thread can update controls, unless you use
Control.InvokeorControl.BeginInvoketo forward updates from other threads, but I don't see the point in your case.
- 为什么需要并行更改颜色?
- 无论如何你不能这样做,因为只有 UI 线程可以更新控件,除非你使用
Control.Invoke或Control.BeginInvoke转发来自其他线程的更新,但我没有看到你的情况。
I suggest you simply do this:
我建议你简单地这样做:
private void btnSistemIzle_Click(object sender, EventArgs e)
{
txtHKB1.BackColor = Color.Yellow;
txtHKB2.BackColor = Color.Yellow;
txtHKB3.BackColor = Color.Yellow;
txtHKB4.BackColor = Color.Yellow;
txtHKB1.Text = "";
txtHKB2.Text = "";
txtHKB3.Text = "";
txtHKB4.Text = "";
SistemIzle("192.168.20.60");
SistemIzle("192.168.20.80");
SistemIzle("192.168.20.100");
SistemIzle("192.168.20.120");
counter2++;
}
回答by sealz
If I understand correctly you are using a WinForm and the texboxes do change accordingly when you ping?
如果我理解正确,您使用的是 WinForm 并且当您 ping 时文本框会相应地更改吗?
To have them set to white before you ping calling this code at the beginning of the method should work. You shouldn't have to seperatly thread it. Are you threading for any specific reason?
在方法开始时调用此代码之前将它们设置为白色应该可以工作。你不应该单独穿线它。您是否出于任何特定原因进行线程处理?
txtHKB1.BackColor = Color.White;
txtHKB2.BackColor = Color.White;
txtHKB3.BackColor = Color.White;
txtHKB4.BackColor = Color.White;
Not sure what else could be causing it, maybe make a method then call it anywhere you need them to change back to white?
不确定还有什么可能导致它,也许创建一个方法然后在任何需要它们变回白色的地方调用它?
private void colorchange()
{
txtHKB1.BackColor = Color.White;
txtHKB2.BackColor = Color.White;
txtHKB3.BackColor = Color.White;
txtHKB4.BackColor = Color.White;
}
and at the start of your other button click just call.
并在您的另一个按钮开始时单击呼叫。
colorchange();

