如果没有选择单选按钮,如何阻止程序进行计算
本文关键字:程序 计算 何阻止 有选择 单选按钮 如果 | 更新日期: 2023-09-27 18:07:18
我对编程真的很陌生。c#是我上的第一门课,我被这个项目困住了。我们必须创建一个程序,该程序将在选择车间单选按钮和位置单选按钮后计算车间的成本。我把一切都安排好了除了一件事。
假设您选择了一个车间,但您没有选择位置。我有它的地方,MessageBox
将显示说"选择一个位置",但我如何阻止程序计算,如果发生这种情况?到目前为止,它只会计算并给出位置量0。我需要它根本不计算。
public partial class frmWorkshopSelector : Form
{
public frmWorkshopSelector()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //When clicking the exit button, the program will close
}
private void btncalc_Click(object sender, EventArgs e)
{
int wsregistration = 0;
int lcost = 0;
const decimal DAYS = 3;
//For the following if statements, depending on what workshop and location is selected,
//their correstponding registration and lodging fees will be displayed
{
if (rbtHandlingStress.Checked == true)
{
wsregistration = 1000;
}
else if (rbtSupervisionSkills.Checked == true)
{
wsregistration = 1500;
}
else if (rbtTimeManagement.Checked == true)
{
wsregistration = 800;
}
else
MessageBox.Show("Please Select a Workshop");
lblTotalCost.Text = "";
lblLodgingCost.Text = "";
lblRegistrationCost.Text = "";
}
{
if (rbtAustin.Checked == true)
{
lcost = 150;
}
else if (rbtChicago.Checked == true)
{
lcost = 225;
}
else if (rbtDallas.Checked == true)
{
lcost = 175;
}
else
{
MessageBox.Show("Please Select a Location");
lblRegistrationCost.Text = " ";
lblTotalCost.Text = " ";
lblLodgingCost.Text = " ";
}
}
lblRegistrationCost.Text = wsregistration.ToString("C");
lblLodgingCost.Text = lcost.ToString("C");
lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C");
}
private void btnReset_Click(object sender, EventArgs e)
{
//unchecks all radio buttons as well as clears out the previous calculations
lblRegistrationCost.Text = "";
lblLodgingCost.Text = "";
lblTotalCost.Text = "";
rbtHandlingStress.Checked = false;
rbtSupervisionSkills.Checked = false;
rbtTimeManagement.Checked = false;
rbtAustin.Checked = false;
rbtChicago.Checked = false;
rbtDallas.Checked = false;
}
}
您必须退出该方法。在else块中增加返回语句。
private void btncalc_Click(object sender, EventArgs e)
{
int wsregistration = 0;
int lcost = 0;
const decimal DAYS = 3;
//For the following if statements, depending on what workshop and location is selected,
//their correstponding registration and lodging fees will be displayed
if (rbtHandlingStress.Checked == true)
{
wsregistration = 1000;
}
else if (rbtSupervisionSkills.Checked == true)
{
wsregistration = 1500;
}
else if (rbtTimeManagement.Checked == true)
{
wsregistration = 800;
}
else
{
lblTotalCost.Text = "";
lblLodgingCost.Text = "";
lblRegistrationCost.Text = "";
MessageBox.Show("Please Select a Workshop");
return;
}
if (rbtAustin.Checked == true)
{
lcost = 150;
}
else if (rbtChicago.Checked == true)
{
lcost = 225;
}
else if (rbtDallas.Checked == true)
{
lcost = 175;
}
else
{
lblRegistrationCost.Text = " ";
lblTotalCost.Text = " ";
lblLodgingCost.Text = " ";
MessageBox.Show("Please Select a Location");
return;
}
lblRegistrationCost.Text = wsregistration.ToString("C");
lblLodgingCost.Text = lcost.ToString("C");
lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C");
}
在函数内的任何地方写一个"return"退出该函数,也许在您显示消息框输入位置后,您键入
return;
这个应该可以。
只需在显示如下消息框后在代码中添加返回语句
public partial class frmWorkshopSelector : Form
{
public frmWorkshopSelector()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close(); //When clicking the exit button, the program will close
}
private void btncalc_Click(object sender, EventArgs e)
{
int wsregistration = 0;
int lcost = 0;
const decimal DAYS = 3;
//For the following if statements, depending on what workshop and location is selected,
//their correstponding registration and lodging fees will be displayed
{
if (rbtHandlingStress.Checked == true)
{
wsregistration = 1000;
}
else if (rbtSupervisionSkills.Checked == true)
{
wsregistration = 1500;
}
else if (rbtTimeManagement.Checked == true)
{
wsregistration = 800;
}
else
MessageBox.Show("Please Select a Workshop");
lblTotalCost.Text = "";
lblLodgingCost.Text = "";
lblRegistrationCost.Text = "";
return;
}
{
if (rbtAustin.Checked == true)
{
lcost = 150;
}
else if (rbtChicago.Checked == true)
{
lcost = 225;
}
else if (rbtDallas.Checked == true)
{
lcost = 175;
}
else
{
MessageBox.Show("Please Select a Location");
lblRegistrationCost.Text = " ";
lblTotalCost.Text = " ";
lblLodgingCost.Text = " ";
return;
}
}
lblRegistrationCost.Text = wsregistration.ToString("C");
lblLodgingCost.Text = lcost.ToString("C");
lblTotalCost.Text = (wsregistration + (lcost * DAYS)).ToString("C");
}
private void btnReset_Click(object sender, EventArgs e)
{ //uncheks all radio buttons as well as clears out the previous calculations
lblRegistrationCost.Text = "";
lblLodgingCost.Text = "";
lblTotalCost.Text = "";
rbtHandlingStress.Checked = false;
rbtSupervisionSkills.Checked = false;
rbtTimeManagement.Checked = false;
rbtAustin.Checked = false;
rbtChicago.Checked = false;
rbtDallas.Checked = false;
}
}
}