【发布时间】:2017-12-01 02:41:38
【问题描述】:
我正在使用 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