0

嗨,我对视觉工作室很陌生。

到目前为止,这是我制作自动取款机示例的代码,我有一个文本框,我输入了一个金额,然后我有这个按钮,我点击信用,它将金额添加到一个名为余额的标签,我还有一个名为借方的按钮将钱从余额中取出。我在 wpf c# 中这样做

到目前为止我有这个。

    namespace BankAccounts
     {
     /// <summary>
     /// Interaction logic for MainWindow.xaml
     /// </summary>
     public partial class MainWindow : Window
     {
       public MainWindow()
    {
        InitializeComponent();
    }

    private double totalamount = 0;
    public string balance1;


    private void buttoncredit_Click(object sender, RoutedEventArgs e)
    {
        totalamount = totalamount + double.Parse(textboxamount.Text)

        balance1 = "Your Balance is: £";

        label2.Content = balance1 + totalamount;

        textboxamount.Clear();



    }

    private void buttondebit_Click(object sender, RoutedEventArgs e)
    {
        if (totalamount - double.Parse(textboxamount.Text) < 0)
        {
            MessageBox.Show("Overdraft Limit is not set please contact Customer Services");
        }

        else
        {

            totalamount = totalamount - double.Parse(textboxamount.Text);

            balance1 = " Your Balance is: £";

            label2.Content = balance1 + totalamount;



            textboxamount.Clear();
        }
    }

    private void listboxtransactions_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {



    }
}

}

4

4 回答 4

3

输入的字符串textboxamount.Text不能被解析为双精度。为避免异常,您可以double.TryParse改用。

double amount;

if(double.TryParse(textboxamount.Text, out amount))
{
    totalamount += amount;
}

此外,label2似乎是Label,你必须使用

label2.Text = balance1 + totalamount;

反而。

于 2013-10-04T23:08:27.283 回答
2

您不能相信您的用户会在您的文本框中准确键入双精度值。
如果输入无法转换为双精度,则 Parse 方法无法避免异常。
相反,double.TryParse方法有机会测试键入的值是否有效地是双精度数。此外,您似乎正在使用货币值,因此使用十进制数据类型可能会更好,并且在构建输出字符串时使用适当的格式来为您的语言环境获取正确的货币字符串。这也将避免双/单数据类型中固有的舍入错误

private decimal totalamount = 0;
public string balance1;

private void buttoncredit_Click(object sender, RoutedEventArgs e)
{
    decimal temp;
    if(decimal.TryParse(textboxamount.Text, out temp))
    {
        totalamount = totalamount + temp;
        balance1 = "Your Balance is: ";
        label2.Content = balance1 + totalamount.ToString("C");    
        textboxamount.Clear();

    }
    else
        MessageBox.Show("Not a valid amount");
}

private void buttondebit_Click(object sender, RoutedEventArgs e)
{
    decimal temp;
    if(decimal.TryParse(textboxamount.Text, out temp))
    {
        if (totalamount - temp < 0)
        {
             MessageBox.Show("Overdraft Limit is not set please contact Customer Services");
        }
        else
        {
             totalamount = totalamount - temp;
             balance1 = " Your Balance is: ";
             label2.Content = balance1 + totalamount.ToString("C");
             textboxamount.Clear();
        }
    }
    else
        MessageBox.Show("Not a valid amount");
}
于 2013-10-04T23:10:37.700 回答
1

问题是 in 中的值textboxamount.Text包含无法转换为双精度的内容。

处理此问题的最佳方法是double.TryParse改用:

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...
于 2013-10-04T23:08:13.163 回答
1

两个主要问题导致此错误:

  • 任何附加文本,例如空格或货币字符。
  • 不正确的本地化设置导致,并被.翻转。

可能包括,但我不记得这是否是错误条件。

于 2013-10-04T23:08:56.857 回答