I am still having difficulty understanding switch. I have visited many sites, but no one explains it enough. I have used switch successfully once, but it was different to what i am trying to do now and cant get my head around it.
I have done this:
Code:
switch (e.KeyCode)
{
case Keys.NumPad1:
tbxDisplay.Text = tbxDisplay.Text + "1";
break;
}
Which enters 1 into my text box when the numpad 1 is pressed. I can understand this as an if statement like:
Code:
if (e.KeyCode == Keys.NumPad1)
{
tbxDisplay.Text = tbxDisplay.Text + "1";
}
That I can understand, effectively the first part of the switch (e.keycode) == the case Keys.Numpad1. So its like the == is being removed.
-------------------------------------------
I am trying a different thing now on another project.
I am building a different type of calculator, the structure is below:
textBox1 - enter first number
comboBox1 - select from x-/+
textBox2 - enter second number
comboBox2 - select from x-/+
textBox3 - enter third number
comboBox3 - select from x-/+
textBox4 - enter fourth number
textBoxTotalCol1 - total of sum depending which operators are selected in the comboBoxes.
Basically, if I type a number in textBox1 and textBox2, the sum is calculated based on the selected operator in comboBox1...and so on. The answer would be entered into textBoxTotalCol1.
The operator is between the textboxes above and below, so comboBox1 (in the middle of the textboxes) calculates textBox1 & 2, combo2 calculates textbox2 & 3 and combo3 is for textbox3 & 4. My intention is to enter enough cases to workout calculation based on the fact that multiplication and division come before plus and minus,
I want to use a switch to make it easier to do the sums based on which operator and how many were selected and which combination. It will calculate when a button is pressed. I have pasted my code below, the red text is where i have the error.
Code:
private void button1_Click(object sender, EventArgs e)
{
//parse all text boxes as doubles
double tbx1 = double.Parse(textBox1.Text);
double tbx2 = double.Parse(textBox2.Text);
double tbx3 = double.Parse(textBox3.Text);
double tbx4 = double.Parse(textBox4.Text);
//create variable for the combo box
string cbx1 = comboBox1.Text;
string cbx2 = comboBox2.Text;
string cbx3 = comboBox3.Text;
double totalcol1 = double.Parse(textBoxTotalCol1.Text);
switch (totalcol1)
{
case cbx1 = "*" & cbx2 = "" & cbx3 = "":
totalcol1 = tbx1 * tbx2;
break;
case cbx1 = "*" & cbx2 = "*" & cbx3 = "":
totalcol1 = tbx1 * tbx2 * tbx3;
break;
}
I read it based on my other switch that worked, that if cbx1 has multiply (x) selected and the other combos are empty, the answer totalcol1 is tbx1 * tbx2.
I think i am missing something.