C# 如何向标签添加动态变化的浮点值
我正在编写这个应用程序,它会将你的电池电量消耗%并定期向用户显示。到目前为止,我已经将其作为控制台应用程序使用,但当我在Windows窗体中重新编写它时,在使用标签显示值时会出现一些问题 编辑:我已经包括了Niranjan Kala指出的变化,因为我使用了不止一个标签,但它仍然不起作用 此外,正如他所要求的,我添加了更多的程序布局,以便提供一些上下文,并可能发现错误 这就是它看起来的样子:C# 如何向标签添加动态变化的浮点值,c#,winforms,visual-studio,C#,Winforms,Visual Studio,我正在编写这个应用程序,它会将你的电池电量消耗%并定期向用户显示。到目前为止,我已经将其作为控制台应用程序使用,但当我在Windows窗体中重新编写它时,在使用标签显示值时会出现一些问题 编辑:我已经包括了Niranjan Kala指出的变化,因为我使用了不止一个标签,但它仍然不起作用 此外,正如他所要求的,我添加了更多的程序布局,以便提供一些上下文,并可能发现错误 这就是它看起来的样子: public partial class Form1 : Form { public Form1()
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
添加此项后,当我连接充电器时,它显示得非常好,但当我断开充电器并输入“else”时,我在其中为标签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;
object BatQuantity=pi[3].GetValue(SystemInformation.PowerStatus,null);
//将我从PowerStatus类获得的值转换为%
float num=(Single.Parse(CantdeBat.ToString())*100;
//显示电池百分比,如果电池已满,则表示电池已满;如果电池未满,则表示电池电量不足
字符串batteryStatus=num.ToString()+“%”;
如果(num==100)
{
电池状态=“-电池电量已满-”;
}
否则如果(num为什么要使用多个标签。您应该只使用一个标签..然后用所需字符串更新它的文本。您确定代码路径中的所有标签都已更新吗?太好了,即使我使用1个标签在其他函数中显示多个状态,但出于某种原因,我在本例中没有这样做。lol。所以,我按照您的建议做了ted,遗憾的是,它仍然没有显示字符串,而是“label4”。我真的不知道该怎么做,甚至不知道编码有错误,所以我很感谢你的任何其他想法。谢谢!我更新了你对原始问题的建议,如果你有任何其他想法,我将非常感谢!你是否尝试从任何背景线程更新它。或者另外。如果你在UI线程上更新它,那么它必须更新标签的文本。如果你在表单上提供更多关于程序流和标签存在的信息,那就更好了。我已经按照你的要求在原始代码中添加了更多的上下文,我对编码是新手,也许我犯了一个像你说的错误。我对线程不太了解g、 编写你的实际代码示例..这个线程代码是否在程序中使用??
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;