简体   繁体   中英

Error 'Input string was not in a correct format' in Label Text

I need to convert one label string to int and another one to float, so I can get the total price with the amount and the unit price.

Here's what I tried:

  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();


        }

    }

When one label text changes, it should make the math again, but everything I tried won't work.

Edit: The labels don't have any text, it gets the user input

<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" />

Could it be the placeholder 'R$'?

We've not seen any input example yet, but hopefully snippet below will get you up and running:

    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!
        }


    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM