2017-11-30 12 views
0

Image of my formは、私はC#でWindowsフォームアプリケーション(.NET Frameworkの)を使用して、Visual Studioの2017 Professionalを使用しています

その内容を倍増からリストボックスを防止します。私は18クレジットのための入力されたコストを表示し、計算ボタンをクリックするとそれらをすべて追加するリストボックスがあります、私は18クレジットを1回だけ表示することができますので、誰かが計算をクリックするようにしたいボタンを2回押すと情報が2回表示されません。 はここだけでも、新しいアイテム

lstTuition.Items.Clear(); 
for (int x = 1; x <= MAX_VALUE; x++) 
{ 
    lstTuition.Items.Add(x + " Credits ~" + " " + (x * dblTuition).ToString("c")); 
} 

を追加する前に、リストボックスをオフに私のコード

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    //Create a double for the amount entered for tuition 
    double dblTuition; 


    //Validate the cost 
    if (double.TryParse(txtCost.Text, out dblTuition)) 
    { 
     //Validate the cost textbox contol 
     if (double.TryParse(txtCost.Text, out dblTuition)) 
     { 

      //Constant for the maximum number 
      const int MAX_VALUE = 18; 

      dblTuition.ToString("c"); 

      //display the tuition 
      for (int x = 1; x <= MAX_VALUE; x++) 
      { 
       lstTuition.Items.Add(x + " Credits ~" + " " + (x * dblTuition).ToString("c")); 
      } 

     } 
     if (dblTuition < 0) 
     { 
      //Display an error message for the cost textbox 
      MessageBox.Show("Invalid Input, Cost Needs to be Greater than Zero."); 

      //Set the cost to zero 
      txtCost.Text = string.Empty; 

      //set focus to cost 
      txtCost.Focus(); 
      txtCost.SelectAll(); 
     } 
    } 
    else 
    { 
     //Display an error message for the Cost textbox 
     MessageBox.Show("Invalid input for Cost."); 

     //Set the cost to zero 
     txtCost.Text = string.Empty; 

     txtCost.Focus(); 
     txtCost.SelectAll(); 
    } 

    txtCost.Focus(); 
    txtCost.SelectAll(); 
} 

答えて

0

だ、あなたのコードを簡略化することができます。

  • dblTuition.ToString("c");フォーマットされた値は、任意の変数に代入されていないため、不必要であると何もしません。

  • txtCost.Focus(); txtCost.SelectAll();は、とにかく最後に行われ、ifelseの中にドロップすることができます。

  • dblTuitionの宣言は、outの式にインライン展開できます。

  • txtCost.Textを2回解析し、if文のロジックが間違っています。

  • 文字列補間を使用して、文字列の作成を簡素化できます。

  • コメントを追加する代わりに、定数の発言名を使用します。悪い名前+コメントよりもむしろ話す名前を使用する方が常に良いです。コードの一部上記

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    //Validate the cost 
    if (Double.TryParse(txtCost.Text, out double dblTuition)) { 
     if (dblTuition > 0) { 
      const int NumberOfCreditsToDisplay = 18; 

      //display the tuition 
      lstTuition.Items.Clear(); 
      for (int x = 1; x <= NumberOfCreditsToDisplay; x++) { 
       lstTuition.Items.Add($"{x} Credits ~ {x * dblTuition:c}"); 
      } 
     } else { 
      //Display an error message for the cost textbox 
      MessageBox.Show("Invalid Input, Cost Needs to be Greater than Zero."); 

      //Set the cost to zero 
      txtCost.Text = String.Empty; 
     } 
    } else { 
     //Display an error message for the Cost textbox 
     MessageBox.Show("Invalid input for Cost."); 

     //Set the cost to zero 
     txtCost.Text = String.Empty; 
    } 
    txtCost.Focus(); 
    txtCost.SelectAll(); 
} 

コメントは、多くの場合、このコードは、多くの場合、より読みやすく、より保守コードにつながる(名前はコメントを置き換えます)別の方法に抽出することができることを示唆しています。

private void btnCalculate_Click(object sender, EventArgs e) 
{ 
    //Validate the cost 
    if (Double.TryParse(txtCost.Text, out double dblTuition)) { 
     if (dblTuition > 0) { 
      DisplayTheTuition(dblTuition); 
     } else { 
      DisplayMessageAndResetCost("Invalid input, Cost needs to be greater than zero."); 
     } 
    } else { 
     DisplayMessageAndResetCost("Invalid input for Cost."); 
    } 
    txtCost.Focus(); 
    txtCost.SelectAll(); 
} 

private void DisplayTheTuition(double dblTuition) 
{ 
    const int NumberOfCreditsToDisplay = 18; 
    lstTuition.Items.Clear(); 
    for (int x = 1; x <= NumberOfCreditsToDisplay; x++) { 
     lstTuition.Items.Add($"{x} Credits ~ {x * dblTuition:c}"); 
    } 
} 

private void DisplayMessageAndResetCost(string message) 
{ 
    MessageBox.Show(message); 
    txtCost.Text = String.Empty; 
} 
関連する問題