简体   繁体   English

如果未选择单选按钮,如何停止计算程序?

[英]How do I stop a program from calculating if no radio button is selected?

I'm really new to programming. 我真的是编程新手。 C# is the first class I've taken and I'm stuck on this a project. C#是我上过的第一堂课,我被困在这个项目上。 We had to create a program that will calculate the cost of workshop after selecting a workshop radiobutton and a location radio button. 我们必须创建一个程序,该程序将在选择车间单选按钮和位置单选按钮后计算车间成本。 I've got everything working the way it's supposed to except for one thing. 除了一件事之外,我已经按照预期的方式进行了所有工作。

Let's say you select a workshop, but you don't select a location. 假设您选择了一个工作室,但没有选择位置。 I have it to where a MessageBox will show up saying to "select a location," but how do I stop the program from calculating if this happens? 我有一个出现MessageBox的地方,说“选择一个位置”,但是如何阻止程序计算这种情况呢? As of now, it will just calculate and give the location amount 0. I need it to not calculate at all. 到目前为止,它只会计算并给出位置数量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;
    }
}

You have to exit from the method. 您必须退出该方法。 Added return statement in else block. 在else块中添加了return语句。

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

writing a "return" anywhere within the function exits that function, maybe after you show the message box to enter location, you type 在函数内的任何位置编写“ return”将退出该函数,也许在显示消息框以输入位置后,键入

return;

and this should do the job. 这应该可以完成工作。

Just Add return statement in your code after showing the message box like below 如下所示显示消息框后,只需在代码中添加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;
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得单选按钮所选项目的文本? - How do I get the text of the radio button selected item? 如何从分组框中选中哪个单选按钮? - How do I get which radio button is checked from a groupbox? 如何设置在向导控件中选中的先前选择的单选按钮? - How do I set previously selected radio button checked in wizard control? 如何使用带浮点值的MVC RadioButtonFor选择正确的单选按钮? - How do I get the correct radio button selected using MVC RadioButtonFor with float values? 如何从单选按钮列表和复选框列表中获取选定的值? - How can I get selected values from radio button list and check box list? 如何保存单选按钮的状态? - How do I save the status of a radio button? 如何在单击按钮时阻止更新面板刷新? - How do I stop an update panel from refreshing on button click? 如何根据选定的单选按钮值传递单选按钮值? - How can i pass radio button values based on the selected one? 如何使用jquery获取选定的asp单选按钮? - how can i get the selected asp radio button using jquery? 如何在不使用GroupBox的情况下为单选按钮提供值并确定选定的单选按钮 - How can I give a radio button a value and determine the selected radio button without using a GroupBox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM