jjmackey9 jjmackey9 - 1 month ago 5
C# Question

how do I loop this code so I can add to the total cost value

how do I loop this code so I can add to the total cost value

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


}
}


}

Answer

add this code:

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

if you simply don't want the Userform to go on, then add at the bottom of 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");
        }

and so the user will have to re-enter valid data and have textbox populated

Comments