如何乘以数量并记录C#

时间:2016-04-11 21:11:50

标签: c# listview windows-phone-8.1 multiplication

我是c#和app developpement的新手。我试图将列表视图中的值乘以用户在文本框中输入的数量。我的代码如下:

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Menu.PassedData data = e.Parameter as Menu.PassedData;

        if (data != null) //If data is not 0 
        {
            PassedData.Add(data); //Increment data in list view

            double tempTotalValue = 0;
            foreach (var record in PassedData)
            {
                tempTotalValue = tempTotalValue + record.Value;
                Totaltxt.Text = String.Format(new CultureInfo("en-GB"),"{0:C}", tempTotalValue);

                textBox3.Text = string.Format("{0:Q2}", record.Value * double.Parse(quanttextBox.Text));    //The code to multiply user input of quantity by each Value produced
            }

            TotalValue = tempTotalValue;
        }
    }

Listview中的文本框,显示每个项目的值

  <TextBox Grid.Column="0" Text="{Binding Value}" IsReadOnly="True" FontSize="15" />

显示总值的文本框

<TextBox x:Name="Totaltxt" HorizontalAlignment="Left" Height="45" Margin="250,461,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="140" TextChanged="Totaltxt_TextChanged" Text="{Binding TotalValue}" />

运行时出错

An exception of type 'System.FormatException' occurred in mscorlib.ni.dll but was not handled in user code

故障排除提示

When converting a string to datetime, parse the string before putting each variable in the date and time object

我想要做的是将listview中生成的每个项目的值乘以用户输入,并相应地更新totalValue,这样基本上数量*列表视图中的每个值。希望这个问题有道理。

1 个答案:

答案 0 :(得分:0)

在尝试在计算中使用之前,将用户输入转换为局部变量会更安全:

double result;
if (double.TryParse(quanttextBox.Text, out result))
{
    textBox3.Text = (record.Value * result).ToString();
}

然后,如果用户没有输入有效的数值,您的代码就不会失败。

然而,您的错误的原因似乎是您尝试将结果格式化为好像是DateTime而不是double。