如何将动态更改的浮点值添加到标签

时间:2015-11-26 05:14:11

标签: c# winforms visual-studio

我正在对此应用进行编码,将您的电池电量提取并定期向用户显示。我已经使它成为控制台应用程序的工作,但是当我在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&#39;我发现这是由充电器状态中的逻辑引起的,所以我添加了这个:

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

当我连接充电器时加上它后显示得非常好,但是当我断开充电器进入&#39;否则&#39;在我为标签4编码条件的地方,问题就开始了。

标签2和标签5中的字符串都会改变,因为我将充电器从笔记本电脑中断开而没有任何问题,问题在于标签4仍然存在。当我断开充电器时,它会停止显示我定义为&#39; -Cargando - &#39;它只是显示了它的名字&#39; label4&#39;。

我被困,有什么想法吗?

我在VS 2015中使用C#。

1 个答案:

答案 0 :(得分:0)

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

File: 0 / Line:6 / Field:1, Bad character (ASCII 0) encountered: field starts with: < c7d��{>
File: 0 / Line:7 / Field:1, Bad character (ASCII 0) encountered: field starts with: < #7��v#>
File: 0 / Line:13 / Field:1, Bad character (ASCII 0) encountered: field starts with: < c7d�1>
File: 0 / Line:17 / Field:1, Bad character (ASCII 0) encountered: field starts with: <�+�d��>
File: 0 / Line:20 / Field:1, Bad character (ASCII 0) encountered: field starts with: <7�J�F�)�>

希望得到这个帮助。