If Statements
// if else
if (fname != "")
{
label_nameOutput.Text = "Hello " + fname + " " + lname + "!";
}
else
{
MessageBox.Show("Rob is amazing.");
}
//switch
switch (expression)
{
case value_1:
statements;
break;
case value_2;
satemetns;
break;
default:
statements;
break;
}
// checkbox
if (choiceRadioButton.Checked) {} else {};
//or
if (choiceRadioButton.Checked == true) {} else {};
// int.TryParse | if successful returens true
int number;
if (int.TryParse(inputTextBox.Text, out number)) {
//code
}
// if selectbox doesnt have a value
// switch statement for value of selectbox
if (listBox_timezones.SelectedIndex != -1)
{
switch (listBox_timezones.SelectedIndex)
{
case 0:
label_timezone_output.Text = "Eastern";
break;
case 1:
label_timezone_output.Text = "Mountain";
break;
case 2:
label_timezone_output.Text = "Pacific";
break;
default:
break;
}
}
else
{
MessageBox.Show("Please select an item.");
}
// grab number from textbox, check if its a number
string tb_input = textBox_enterint.Text;
int tb_input_int;
if (int.TryParse(tb_input, out tb_input_int))
{
switch (tb_input_int)
{
case 1:
label_answer.Text = "I";
break;
case 2:
label_answer.Text = "II";
break;
case 3:
label_answer.Text = "III";
break;
case 4:
label_answer.Text = "IV";
break;
case 5:
label_answer.Text = "V";
break;
case 6:
label_answer.Text = "VI";
break;
case 7:
label_answer.Text = "VII";
break;
case 8:
label_answer.Text = "VIII";
break;
case 9:
label_answer.Text = "IX";
break;
case 10:
label_answer.Text = "X";
break;
default:
MessageBox.Show("Your entry is invalid.");
textBox_enterint.Text = "";
break;
}
}
else
{
MessageBox.Show("Please enter an integer only.");
textBox_enterint.Text = "";
}
// get number from textbox, check if number, do stuff acording the value.
const int PRICE = 99;
double discount;
string tb_input = textBox_numberOfPackages.Text;
int amount;
string discount_display;
if (int.TryParse(tb_input, out amount))
{
if (amount < 10)
{
discount = 1;
discount_display = "None";
}
else if (amount < 20)
{
discount = 0.2;
discount_display = "20%";
}
else if (amount < 50)
{
discount = 0.3;
discount_display = "30%";
}
else if (amount < 100)
{
discount = 0.4;
discount_display = "40%";
}
else
{
discount = 0.5;
discount_display = "50%";
}
label_discount.Text = discount_display;
label_total.Text = "$" + ((PRICE * amount) * discount);
}
else
{
MessageBox.Show("Please enter an integer only.");
textBox_numberOfPackages.Text = "";
textBox_numberOfPackages.Focus();
}