C# 如何循环此代码,以便增加总成本值
如何循环此代码,以便添加到总成本值中C# 如何循环此代码,以便增加总成本值,c#,winforms,visual-studio,textbox,decimal,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 voi
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");
}
}
}
添加此代码:
private void totalCostTextBox_TextChanged(object sender, EventArgs e)
{
if (totalCostTextBox.Text == "€ 0,00")
MessageBox.Show("hey!");
}
如果您只是不想继续使用Userform,请在calculateBtn\u Click()的底部添加:
因此,用户必须重新输入有效数据并填充文本框您的目标是什么:Winforms、WPF、ASP。。?始终正确标记您的问题!-您的格式设置已关闭。请不要用感叹号。一个“?”就行了查看文本框的TextChanged
事件。对不起,这是我的第一个问题。我找到了如何获取我想要的消息框。但是,我如何循环这个代码,以便将其添加到总成本中呢?现在听起来好像是一个完全不同的问题。对不起。你能帮忙吗?帮帮我,伙计。你知道你想。你是个好人谢谢。你知道我将如何循环这个来增加总成本的价值吗。仿佛user@jjmackey9对不起,我不明白你的意思。你能详细解释一下吗?
//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");
}