在运行时更改TextBox BackColor

时间:2012-02-03 13:30:01

标签: c# winforms multithreading textbox

我有4个文本框和1个按钮。 按下按钮时,它会ping 4个IP地址,然后根据ping状态更改文本框颜色。

我想要做的是,当按下按钮时,所有文本框背面颜色在ping开始之前变为白色。

我编写了以下代码,但它没有用。

我的代码:

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ı";
                }
            }

我做错了什么? 我最诚挚的问候......

2 个答案:

答案 0 :(得分:4)

这段代码没有多大意义。您正在生成两个线程只是为了更改由不同线程拥有的控件的颜色?出于多种原因这是错误的:

  1. 为什么需要并行更改颜色?
  2. 无论如何你不能这样做,因为只有UI线程可以更新控件,除非你使用Control.InvokeControl.BeginInvoke来转发来自其他线程的更新,但是我没有看到你的情况。
  3. 我建议你这样做:

            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++;
            }
    

答案 1 :(得分:1)

如果我理解你正在使用WinForm并且你在ping时texbox会相应改变吗?

要在ping之前将它们设置为白色,请在方法开头调用此代码。你不应该分开线程。您是否因任何特定原因进行了线程化?

txtHKB1.BackColor = Color.White;
txtHKB2.BackColor = Color.White;
txtHKB3.BackColor = Color.White;
txtHKB4.BackColor = Color.White;

不确定还有什么可能导致它,也许制作一个方法,然后在需要它们的任何地方调用它来改回白色?

private void colorchange()
{
    txtHKB1.BackColor = Color.White;
    txtHKB2.BackColor = Color.White;
    txtHKB3.BackColor = Color.White;
    txtHKB4.BackColor = Color.White;
}

在您的其他按钮开始时,请点按即可。

colorchange();