【问题标题】:Preventing a list box from doubling its contents防止列表框的内容加倍
【发布时间】:2017-12-01 02:41:38
【问题描述】:

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

【问题讨论】:

    标签: c# visual-studio listbox


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2021-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-03
      • 1970-01-01
      相关资源
      最近更新 更多