Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在运行时更改文本框背景色_C#_Winforms_Multithreading_Textbox - Fatal编程技术网

C# 在运行时更改文本框背景色

C# 在运行时更改文本框背景色,c#,winforms,multithreading,textbox,C#,Winforms,Multithreading,Textbox,我有4个文本框和1个按钮。 按下按钮时,它将ping 4个ip地址,然后根据ping状态更改文本框的颜色 我想做的是,当按下按钮时,在ping开始之前,所有文本框的背景颜色都会变为白色 我写了下面的代码,但它不起作用 我的代码: public void Clear1() { txtHKB1.BackColor = Color.Yellow; txtHKB2.BackColor = Color.Yellow;

我有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ı";
                }
            }
public void Clear1()
{
txtHKB1.BackColor=颜色.黄色;
txtHKB2.BackColor=颜色.黄色;
txtHKB3.BackColor=颜色.黄色;
txtHKB4.BackColor=颜色.黄色;
}
公共空间2()
{
txtHKB1.Text=“”;
txtHKB2.Text=“”;
txtHKB3.Text=“”;
txtHKB4.Text=“”;
}
私有void btnConnect_单击(对象发送者,事件参数e)
{
//b、 巴格兰(“192.168.20.50”、“9050”);
}
私有void btnsistimize_单击(对象发送者,事件参数e)
{
线程th1=新线程(新线程开始(Clear1));
螺纹th2=新螺纹(新螺纹起点(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”);
计数器2++;
}
公共系统分析(字符串ip)
{
尝试
{
IPAddress IPAddress=Dns.GetHostEntry(ip).AddressList[0];
//对于(int i=0;i<3;i++)
//{
System.Net.NetworkInformation.Ping Ping=新系统.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply PingReply=ping.Send(ipAddress);
计数器++;
//Show(pingReply.Buffer.Count().ToString()+pingReply.RoundtripTime.ToString())
//+pingReply.Options.Ttl.ToString()+pingReply.Status.ToString());
//系统线程线程睡眠(100);
//}
}
抓住
{
//MessageBox.Show(“Başarısız Girişim!”);
失败++;
}
if(计数器%4==1&&fail==0)
{
txtHKB1.BackColor=Color.Green;
txtHKB1.Text=“雅音达”;
}
if(计数器%4==1&&fail==1)
{
失败=0;
txtHKB1.BackColor=颜色.红色;
txtHKB1.Text=“Kapalı”;
}
if(计数器%4==2&&fail==0)
{
txtHKB2.BackColor=Color.Green;
txtHKB2.Text=“雅音达”;
}
if(计数器%4==2&&fail==1)
{
失败=0;
txtHKB2.BackColor=Color.Red;
txtHKB2.Text=“Kapalı”;
}
if(计数器%4==3&&fail==0)
{
txtHKB3.BackColor=Color.Green;
txtHKB3.Text=“雅音达”;
}
if(计数器%4==3&&fail==1)
{
失败=0;
txtHKB3.BackColor=Color.Red;
txtHKB3.Text=“Kapalı”;
}
if(计数器%4==0&&fail==0)
{
txtHKB4.BackColor=Color.Green;
txtHKB4.Text=“雅音达”;
}
if(计数器%4==0&&fail==1)
{
失败=0;
txtHKB4.BackColor=颜色.红色;
txtHKB4.Text=“Kapalı”;
}
}
我做错了什么?
向您致以最诚挚的问候……

如果我理解正确,您正在使用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;
}
在另一个按钮的开始处,单击just call

colorchange();

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

  • 为什么需要同时更改颜色
  • 无论如何,您不能这样做,因为只有UI线程可以更新控件,除非您使用
    Control.Invoke
    Control.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++;
            }
    

    嗨,当我在方法开头写代码时,它们也不会改变颜色…@Un_NatMenDim当我在方法开头使用代码时,它对我有效。你想让他们在按下按钮时变为白色吗?并一直保持这种方式,直到他们收到ping状态?您的代码将它们设置为黄色?(这是否应该说为白色?)如果在您将它们设置为白色后调用,您将永远不会看到白色的文本框。如果文本框不是白色,它们是什么颜色?