2012-02-03 121 views
0

我有4個文本框和1個按鈕。 當按下按鈕時,它會ping 4個IP地址,然後根據ping狀態更改文本框顏色。運行時更改TextBox BackColor

我想要做的是,當按下按鈕時,所有文本框的背景顏色在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ı"; 
       } 
      } 

我做錯了什麼? 我最好的問候...

+0

的文本框,如果它們是什麼顏色不是白色的? – LarsTech 2012-02-03 14:04:04

回答

5

此代碼沒有多大意義。你生成兩個線程只是爲了改變不同線程擁有的控件的顏色?這是錯誤的,原因很多:

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

如果我理解正確,你使用的是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(); 
+0

嗨,他們不改變顏色,當我在方法的開始寫... – 2012-02-03 13:50:31

+0

@Un_NatMenDim當我在方法的開始使用該代碼時,它適用於我。你想讓它們在按下按鈕時變成白色?並保持這種方式,直到他們收到ping狀態? – sealz 2012-02-03 13:51:40

+0

是的,但我沒有其他的按鈕,只有一個來處理它... – 2012-02-03 14:05:06