简体   繁体   English

在运行时更改 TextBox BackColor

[英]Change TextBox BackColor in run time

I've 4 textboxes and 1 button.我有 4 个文本框和 1 个按钮。 When the button is pressed, it pings the 4 ip addresses, then changes the textboxes color according to ping status.当按下按钮时,它会 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...我最诚挚的问候...

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:这是错误的,原因有很多:

  1. Why would you need to change the color in parallel?为什么需要并行更改颜色?
  2. You cannot do it like this anyway, because only the UI thread can update controls, unless you use Control.Invoke or Control.BeginInvoke to forward updates from other threads, but I don't see the point in your case.无论如何你不能这样做,因为只有 UI 线程可以更新控件,除非你使用Control.InvokeControl.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++;
        }

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM