簡體   English   中英

如果未選擇單選按鈕,如何停止計算程序?

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

我真的是編程新手。 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塊中添加了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");
}

在函數內的任何位置編寫“ return”將退出該函數,也許在顯示消息框以輸入位置后,鍵入

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

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM