2017-09-17 6 views
0

私は単純なストレートライン償却アプリケーションをやっています。私のリストボックスには、年と減価償却額が表示されます。同じリストボックスで、「合計」を加えてすべての減価償却額を合計します。私はどのようにアプリのように見えるかの写真を追加しました。私たちはFORループを使用する必要があります。これについてリストボックスにすべてのアイテムを追加し、同じリストボックスに「合計」を表示するには

private void calcButton_Click_1(object sender, EventArgs e) 
{ 
    //Declare Vairables 
    double cost, salvage, depreciation; 
    int usefulLife; 
    int total = 0; 


    //grab data 
    double.TryParse(assetCostTextBox.Text, out cost); 
    double.TryParse(salvageValueTextBox.Text, out salvage); 
    int.TryParse(usefulLifeNumericUpDown.Text, out usefulLife); 

    //Print heading 
    string formatCode = "{0,7}{1,20}"; 
    depreciationScheduleListBox.Items.Add(string.Format(formatCode, 
    "Years:", "Depreciation:")); 
    depreciationScheduleListBox.Items.Add(""); 

    //use for loop to calculate deprecation value of each year 
    int iterations = 0; 
    for (iterations = 1; iterations <= usefulLife; iterations += 1) 
    { 
     depreciation = (cost - salvage) * 1/usefulLife; 
     depreciationScheduleListBox.Items.Add(string.Format(formatCode, 
     iterations, depreciation.ToString("C2"))); 


    } 

Here is a picture of the app

+2

あなたの宿題をする場所を人が得られる場所ではありません。あなたはあなたが思っていることを行い、それがうまくいかない場合は、あなたが何をしたかを示し、その結果があなたのニーズを満たしていないことを説明します。 – jmcilhinney

答えて

0

どのように?

double total = depreciationScheduleListBox.Items.Cast<string>().Select((string item) => Convert.ToDouble(System.Text.RegularExpressions.Regex.Match(item, "Depreciation: \\$(.+)").Groups[1].Value)).ToArray().Sum(); 

listBoxの項目をIEnumerableに変換しました。 それで、数字でなければならないDepreciation: $の後に何が得られましたか? 次に、この文字列の出力をDoubleに変換し、最終的にdouble[]を得ました。

関連する問題