防止列表框加倍其内容

时间:2017-11-30 17:29:02

标签: c# visual-studio listbox

Image of my form

我正在使用Visual Studio 2017 Professional,在C#中使用Windows窗体应用程序(.NET Framework)。我有一个列表框,显示18个学分的输入成本,并在您单击计算按钮时将它们全部添加在一起,我想要做的是使它只能显示18个学分,所以如果有人点击计算按钮两次它不会显示两次信息。 这是我的代码

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();
}

1 个答案:

答案 0 :(得分:0)

在添加新项目之前,请清除列表框

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

此外,您的代码可以简化。

  • dblTuition.ToString("c");是多余的,不执行任何操作,因为格式化的值未分配给任何变量。

  • 无论如何
  • txtCost.Focus(); txtCost.SelectAll();在最后完成,可以放在ifelse内。

  • dblTuition表达式中可以内联out的声明。

  • 您正在解析txtCost.Text两次,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;
}