2015-11-26 160 views
0

我正在編寫此應用程序,它可以拉動電池電量並定期向用戶顯示。我已經將它應用到了控制檯應用程序,但是當我將它重新寫入Windows窗體時,它使我在使用標籤顯示值時遇到了一些問題。如何將動態更改的浮動值添加到標籤

編輯:我已經包括了變化NIRANJAN卡拉指出,因爲我是用多個標籤這一點,但它仍然無法正常工作

此外,作爲他的要求,我已經加入更多節目佈局,以提供一些背景,也許發現錯誤

這是什麼樣子:

public partial class Form1 : Form 
{ 

public Form1() 

{ InitializeComponent(); 

// Imagine all the visual stuff going here (icons and menus, etc.) 

//I start the thread 
batThing = new Thread(new ThreadStart(batThing)); 
batThing.Start(); 
} 


private void Form1_Load(object sender, EventArgs e) 
    { 
     Type power = typeof(PowerStatus); 
     PropertyInfo[] pi = power.GetProperties(); 


      #region Cargador 


     //0 = PowerLineStatus --> Charger on or not? 
     object EdeCargador = pi[0].GetValue(SystemInformation.PowerStatus, null); 

     //turns charger state into int 
     int edc = Convert.ToInt32(EdeCargador); 

     int On = Convert.ToInt32(PowerLineStatus.Online); 
     On = 1; 
     int Off = Convert.ToInt32(PowerLineStatus.Offline); 
     Off = 0; 
     int Unk = Convert.ToInt32(PowerLineStatus.Unknown); 
     Unk = 255; 

     if (edc == On) 
     { 
      string CargadorConectado = "-Cargador Conectado-"; 
      label2.Text = CargadorConectado; 
      string EdeBateria2 = "-Cargando-"; 
      label4.Text = EdeBateria2; 
     } 
     else 
     { 
      string CargadorDesconectado = "-Cargador Desconectado-"; 
      label2.Text = CargadorDesconectado; 
      #endregion 

      #region Cantidad de Bateria 

      //3 = BatteryLifePercent --> tells the % of bat available 
      object CantdeBat = pi[3].GetValue(SystemInformation.PowerStatus, null); 

      //string to float , then * 100 to get a % 
      float num = (Single.Parse(CantdeBat.ToString())) * 100; 

      // shows a % and says if battery is full or low 
      string EdeBateria = num.ToString() + "%"; 

      if (num == 100) 
      { 
       EdeBateria = "-Batería Completa-"; 
       label4.Text = EdeBateria; 
      } 
      else if (num <= 25) 
      { 
       EdeBateria = "-Batería Baja. Conecte el cargador-"; 
       label4.Text = EdeBateria; 

      } 
      else if (num > 25 & num < 100) 
      { 
       //nada 
      } 

      if (num <= 0) 
      { 
       EdeBateria = "No tenes bateria gil"; 
       label4.Text = EdeBateria; 
      } 



     } 
     #endregion 

     #region Tiempo Restante 
     //4 = BatteryLifeRemaining --> Indicates the remaining battery time 
     object TdeBat = pi[4].GetValue(SystemInformation.PowerStatus, null); 

      double tiempobat = (Double.Parse(TdeBat.ToString())); 

      if (tiempobat == -1) 
      { 
       string NoHayBat = "El equipo no está operando a través de una batería"; 
       label5.Text = NoHayBat; 
      } 
      else if (tiempobat != -1) 
      { 
       //gets time in seconds and turns it into hours , min and secs 
       TimeSpan t = TimeSpan.FromSeconds(tiempobat); 
       string TiempoRestante = string.Format("El tiempo de uso restante es de: {0:D1} horas, {1:D2} minutos y {2:D2} segundos", 
       t.Hours, 
       t.Minutes, 
       t.Seconds 
       ); 

       label5.Text = TiempoRestante ; 


      } 
      #endregion 

    } // fin de form1 

編輯:我發現在從一種方法跳轉到另一種方法時會遇到一些問題,例如,我原來的問題是「標籤4」,我發現這是由於充電器狀態的邏輯,所以我增加這樣的:

string EdeBateria2 = "-Cargando-"; 
      label4.Text = EdeBateria2; 

添加此時,我有充電器連接它顯示得這麼好後,但是當我斷開進入「其他」其中I編碼的問題開始對標籤4的條件句的充電器。

標籤2和標籤5中的字符串發生了變化,因爲我將充電器從筆記本電腦上斷開,沒有任何問題,問題在於標籤4仍然存在。當我斷開充電器時,它會停止顯示我定義爲'-Cargando-'的信息,並且只顯示它的名稱'label4'。

我卡住了,有什麼想法嗎?

我使用C#在VS 2015

+1

你爲什麼使用多個標籤。你應該只使用一個標籤..然後用你想要的字符串更新它的文本。 –

+0

你確定所有的標籤都在你的代碼路徑中更新了嗎? – Kayani

回答

0

如果你想顯示狀態,那麼它應該是這樣的。創建電池狀態,然後更新到標籤。檢查此示例片段,然後根據您的要求更新它。

object BatQuantity= pi[3].GetValue(SystemInformation.PowerStatus, null); 

//convert the value I get from PowerStatus class to % 
float num = (Single.Parse(CantdeBat.ToString())) * 100; 

//shows battery % and if battery is full says it's full if not it says it's low 
string batteryStatus = num.ToString() + "%"; 
if (num == 100) 
{ 
    batteryStatus = "-Full Battery-"; 
} 
else if (num <= 25) 
{   
    batteryStatust = "-Low Battery-"; 
}    
label4.Text = batteryStatust; 

希望得到這個幫助。

+0

好極了,即使我使用1個標籤來顯示其他函數中的多個狀態,但在這種情況下,由於某種原因,我不這麼做。所以,我做了你的建議,可惜它仍然沒有顯示字符串,而是'label4'。我真的不知道該怎麼做,甚至沒有意識到編碼的錯誤,所以我會很感激任何其他的想法。謝謝! –

+0

我將你的建議更新爲原始問題,如果你有更多的想法,我會很感激! –

+0

你想從任何後臺線程更新它。還是其他什麼。如果你在UI線程上更新它,那麼它必須更新標籤的文本。如果您在表單上提供有關程序流程和標籤存在的更多信息會更好。 –