如何将动态变化的浮点值添加到标签中

本文关键字:添加 标签 动态 变化 | 更新日期: 2023-09-27 18:24:33

我正在编写这个应用程序,它可以提取你的电池百分比,并定期向用户显示。到目前为止,我已经把它作为一个控制台应用程序,但当我在Windows窗体中重写它时,它在使用标签显示值时会给我带来一些问题。

编辑:我已经包含了Niranjan Kala指出的更改,因为我为此使用了多个标签,但它仍然不起作用

此外,正如他所要求的,我添加了更多的程序布局,以便提供一些上下文,并可能发现错误

这就是它的样子:

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;

添加后,当我连接充电器时,它显示得很好,但当我断开充电器时,进入"其他",在那里我为标签4编码了条件,问题就开始了。

当我将充电器从笔记本电脑上断开时,标签2和标签5中的字符串都会发生正常变化,没有任何问题,标签4仍然存在问题。当我断开充电器时,它停止显示我定义为"-Cargando-"的信息,只显示其名称"label4"。

我被卡住了,有什么想法吗?

我在VS 2015中使用C#。

如何将动态变化的浮点值添加到标签中

如果您想显示状态,那么它应该是这样的。创建电池状态,然后更新到标签。检查这个示例片段,然后根据您的要求进行更新。。

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;

希望得到帮助。