[英]Label conversion returns error “input string was not in a correct format. ”
[英]Error 'Input string was not in a correct format' in Label Text
我需要將一個標簽字符串轉換為int,再將另一個標簽字符串轉換為float,這樣我才能獲得包含金額和單價的總價格。
這是我嘗試過的:
private void EntQtde_TextChanged(object sender, TextChangedEventArgs e)
{
/**/
if(entRSVenda.Text != null)
{
float RSVenda = float.Parse(entRSVenda.Text);
int Qtde = int.Parse(entQtde.Text);
lblValorTot.Text = (Qtde * RSVenda).ToString();
//lblValorTot.Text = ((float.Parse(entRSVenda.Text)) * int.Parse(entQtde.Text)).ToString();
}
}
private void EntRSVenda_TextChanged(object sender, TextChangedEventArgs e)
{
/**/
if (entQtde.Text != null)
{
float RSVenda = float.Parse(entRSVenda.Text);
int Qtde = int.Parse(entQtde.Text);
//lblValorTot.Text = ((float.Parse(entRSVenda.Text)) * int.Parse(entQtde.Text)).ToString();
lblValorTot.Text = (Qtde * RSVenda).ToString();
}
}
當一個標簽文本更改時,它應該重新進行數學運算,但是我嘗試的所有操作均不起作用。
編輯:標簽沒有任何文本,它得到用戶輸入
<Label Text="" x:Name="lblValorTot"/>
<Entry x:Name="entQtde" Placeholder="Quantidade" Keyboard="Numeric" TextChanged="EntQtde_TextChanged"/>
<Entry x:Name="entRSVenda" Placeholder="Valor unitário (R$)" TextChanged="EntRSVenda_TextChanged" />
可以是占位符“ R $”嗎?
我們還沒有看到任何輸入示例,但是希望下面的代碼片段可以幫助您啟動並運行:
if (entQtde.Text != null)
{
int Qtde;
bool canParse = int.TryParse(entQtde.Text, out Qtde);
if(canParse){
lblValorTot.Text = (Qtde * RSVenda).ToString();
}else{
// invalid input - do something!
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.