Input string was not in a correct format, “double.parse(textbox.text);”

后端 未结 4 1190
栀梦
栀梦 2021-01-28 10:45

Hi am quite new to visual studios.

This is my code so far to make an example of an atm, i have a text box and i put an amount in and then i have this button where i clic

4条回答
  •  轮回少年
    2021-01-28 11:25

    The problem is that the value in textboxamount.Text contains something that can't be converted to a double.

    The best way to handle this is to use double.TryParse instead:

    private void buttoncredit_Click(object sender, RoutedEventArgs e)
    {
        double newAmount;
        if(!double.TryParse(textboxamount.Text, out newAmount))
        {
            // The input is wrong - handle that
            MessageBox.Show("Please enter a valid amount");
            textboxamount.Focus();
            return;
        }
    
        totalamount += newAmount;
        balance1 = "Your Balance is: £";
        label2.Content = balance1 + totalamount;
        // .. Your code...
    

提交回复
热议问题