在运行时更改TextBox背景颜色

本文关键字:背景 颜色 TextBox 运行时 | 更新日期: 2023-09-27 18:22:26

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

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

在运行时更改TextBox背景颜色

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

  1. 为什么你需要同时改变颜色
  2. 无论如何,您都不能这样做,因为只有UI线程可以更新控件,除非您使用Control.InvokeControl.BeginInvoke转发来自其他线程的更新,但我认为您的情况没有意义

我建议你简单地这样做:

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

如果我理解正确,您使用的是WinForm,并且在ping时texboxes会相应地更改?

在方法开始时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();