如何循环此代码,以便我可以添加总成本值

时间:2016-10-08 15:14:28

标签: c# winforms visual-studio textbox decimal

如何循环此代码以便我可以添加总费用值?

using System.Threading.Tasks;
using System.Windows.Forms;

namespace Workshop_Selector
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void calculateBtn_Click(object sender, EventArgs e)
        {


            // Declaring variables to zero so they can found in a list
            decimal registrationFee = 0;
            decimal days = 0;
            decimal lodgingFee = 0;



            if (workshopListBox.SelectedIndex != -1 && locationListBox.SelectedIndex != -1) //when list items selected are not below zero
            {


                //From workshop list data inputs calculate the variables
                switch (workshopListBox.SelectedIndex)
                {


                    case 0:              // in the first case of "Handling Stress" the workshop lasts for 3 days and costs 1000 euro
                        days = 3;
                        registrationFee = 1000;
                        break; //Case finisher

                    case 1:                 // in the second case of "Time Mgnt" the workshop lasts for 3 days and costs 800 euro
                        days = 3;
                        registrationFee = 800;
                        break;

                    case 2:                 // in the third case of "Supervision skills" the workshop lasts for 3 days and costs 1500 euro
                        days = 3;
                        registrationFee = 1500;
                        break;

                    case 3:                 // in the fourth case of "Negotiation" the workshop lasts for 5 days and costs 1300 euro
                        days = 5;
                        registrationFee = 1300;
                        break;

                    case 4:                 // in the fifth case of "How to Interview" the workshop lasts for 3 days and costs 500 euro
                        days = 1;
                        registrationFee = 500;
                        break;

                }
                //From location list data inputs calculate the variables


                switch (locationListBox.SelectedIndex)
                {


                    case 0:              //In the first case in the location list lodging fee per day in Galway is 150 euro
                        lodgingFee = 150;
                        break;

                    case 1:              //In the second case in the location list lodging fee per day in Dublin is 225 euro
                        lodgingFee = 225;
                        break;

                    case 2:              //In the third case in the location list lodging fee per day in Limerick is 175 euro
                        lodgingFee = 175;
                        break;

                    case 3:              //In the fourth case in the location list lodging fee per day in Cork is 300 euro
                        lodgingFee = 300;
                        break;

                    case 4:              //In the fifth case in the location list lodging fee per day in Athlone is 175 euro
                        lodgingFee = 175;
                        break;

                    case 5:              //In the sixth case in the location list lodging fee per day in Ennis is 150 euro
                        lodgingFee = 150;
                        break;
                }

            } 


            //end of if statements and variables

            // Illustrate results
            costLabel.Text = "Registration: " + registrationFee.ToString("c") +
                " \nLodging Fee: " + lodgingFee.ToString("c") + " x " +
                days.ToString() + " days = " + (lodgingFee * days).ToString("c");

            totalCostTextBox.Text = (registrationFee + (lodgingFee * days)).ToString("c");


        }
    }

}

1 个答案:

答案 0 :(得分:0)

添加以下代码:

    private void totalCostTextBox_TextChanged(object sender, EventArgs e)
    {
        if (totalCostTextBox.Text == "€ 0,00")
            MessageBox.Show("hey!");
    }

如果您只是不想让Userform继续播放,请在calculateBtn_Click()的底部添加:

       //end of if statements and variables

        // Illustrate results
        costLabel.Text = "Registration: " + registrationFee.ToString("c") +
            " \nLodging Fee: " + lodgingFee.ToString("c") + " x " +
            days.ToString() + " days = " + (lodgingFee * days).ToString("c");

        decimal totalCost = registrationFee + (lodgingFee * days);
        if (totalCost == 0)
        {
            MessageBox.Show("total cost was zero! Re-Enter data");
        }
        else
        {
            totalCostTextBox.Text = totalCost.ToString("c");
        }

因此用户必须重新输入有效数据并填充文本框