Reputation: 151
I am trying to display total in textBox1
how do I write code which will add the total value of sub1
, sub2
, sub3
and display in texBox1
. mealsub = sub1 + sub2 + sub3
.
I continue to receive an error.
And also when I check mark one, how do I give it a specific value?
if (radioButton1.Checked)
drink = "";
else if (radioButton2.Checked)
drink = "";
else if (radioButton3.Checked)
drink = "";
private void button1_Click(object sender, EventArgs e)
{
string drink = String.Empty;
string burger = String.Empty;
QtextBox1.Enabled = false;
QtextBox2.Enabled = false;
QtextBox3.Enabled = false;
StextBox1.Enabled = false;
StextBox2.Enabled = false;
StextBox3.Enabled = false;
if (checkBox1.Checked) //== true)
{
//QtextBox1.Enabled = true;
//burger += "Cheese Burger \n";
int q1;
int.TryParse(QtextBox1.Text, out q1);
//double q1 = double.Parse(QtextBox1.Text);
double sub1 = q1 * 4.99; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
StextBox1.Enabled = true;
StextBox1.Text = sub1.ToString("c");
}
if (checkBox2.Checked)
{
//QtextBox2.Enabled = true;
//burger += "Fat Burger \n";
//double q2 = double.Parse(QtextBox2.Text);
int q2;
int.TryParse(QtextBox1.Text, out q2);
double sub2 = q2 * 7.99; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
StextBox2.Enabled = true;
StextBox2.Text = sub2.ToString("c");
}
if (checkBox3.Checked)
{
//QtextBox3.Enabled = true;
//burger += "Veggie BUrger \n";
//double q3 = double.Parse(QtextBox3.Text);
int q3;
int.TryParse(QtextBox1.Text, out q3);
double sub3 = q3 * 5.99; //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
StextBox3.Enabled = true;
StextBox3.Text = sub3.ToString("c");
}
// get selected drink
if (radioButton1.Checked)
drink = "";
else if (radioButton2.Checked)
drink = "";
else if (radioButton3.Checked)
drink = "";
textBox1.Text = mealsub.ToString();
double tax = .098;
string taxtotal = mealsub + tax;
textBox2.Text = tax.ToString();
textBox3.Text = taxtotal.ToString();
}
Upvotes: 0
Views: 126
Reputation: 322
In your code, the scope of the variables sub1, sub2, and sub3 is limited to the if blocks in which they are created.
if (checkBox1.Checked) //== true)
{
int q1;
int.TryParse(QtextBox1.Text, out q1);
double sub1 = q1 * 4.99; //sub1 being created here
StextBox1.Enabled = true;
StextBox1.Text = sub1.ToString("c");
}//sub1 does not exist here
if (checkBox2.Checked)
{
int q2;
int.TryParse(QtextBox1.Text, out q2);
double sub2 = q2 * 7.99; //sub2 being created here
StextBox2.Enabled = true;
StextBox2.Text = sub2.ToString("c");
}//sub2 does not exist here
if (checkBox3.Checked)
{
int q3;
int.TryParse(QtextBox1.Text, out q3);
double sub3 = q3 * 5.99; //sub3 being created here
StextBox3.Enabled = true;
StextBox3.Text = sub3.ToString("c");
}//sub3 does not exist here
Which means your sub1 variable will be destroyed as soon as the execution leaves the first if block. In order to fix that, you must define these variables outside the if blocks and preferably right after the function is called. So that they can be accessed inside the function. i.e.
private void button1_Click(object sender, EventArgs e)
{
string drink = String.Empty;
string burger = String.Empty;
double sub1=0 ,sub2=0 ,sub3=0;
//remaining code
One more thing in your code is that I cannot see the mealsub variable initialized anywhere. So you should have something like this after your if blocks
// get selected drink
if (radioButton1.Checked)
drink = "";
else if (radioButton2.Checked)
drink = "";
else if (radioButton3.Checked)
drink = "";
double mealsub=sub1+sub2+sub3;
textBox1.Text = mealsub.ToString();
double tax = .098;
string taxtotal = mealsub + tax;
textBox2.Text = tax.ToString();
textBox3.Text = taxtotal.ToString();
textBox1.Text = (sub1 + sub2 + sub3).ToString();
}
Upvotes: 0
Reputation:
Two issues.
Declare sub1,..,sub3 in outer scope.
double sub1,sub2,sub3; sub1 =0; sub2 = 0; sub3 = 0; // then go to the block
textBox1.Text = (sub1 + sub2 + sub3).ToString(); // sub1, sub2 and sub3 are double type. You have to convert it to string
Upvotes: 0
Reputation: 526
You just need to declare sub1,..,sub3 in outer scope. This means the following code should work:
private void button1_Click(object sender, EventArgs e)
{
string drink = String.Empty;
string burger = String.Empty;
QtextBox1.Enabled = false;
QtextBox2.Enabled = false;
QtextBox3.Enabled = false;
StextBox1.Enabled = false;
StextBox2.Enabled = false;
StextBox3.Enabled = false;
double sub1 = 0,sub2 = 0,sub3 = 0;
if (checkBox1.Checked) //== true)
{
int q1;
int.TryParse(QtextBox1.Text, out q1);
sub1 = q1 * 4.99;
StextBox1.Enabled = true;
StextBox1.Text = sub1.ToString("c");
}
if (checkBox2.Checked)
{
int q2;
int.TryParse(QtextBox1.Text, out q2);
sub2 = q2 * 7.99;
StextBox2.Enabled = true;
StextBox2.Text = sub2.ToString("c");
}
if (checkBox3.Checked)
{
int q3;
int.TryParse(QtextBox1.Text, out q3);
sub3 = q3 * 5.99;
StextBox3.Enabled = true;
StextBox3.Text = sub3.ToString("c");
}
// get selected drink
if (radioButton1.Checked)
drink = "";
else if (radioButton2.Checked)
drink = "";
else if (radioButton3.Checked)
drink = "";
textBox1.Text = mealsub.ToString();
double tax = .098;
string taxtotal = mealsub + tax;
textBox2.Text = tax.ToString();
textBox3.Text = taxtotal.ToString();
textBox1.Text = (sub1 + sub2 + sub3).ToString();
}
For further information about scope visit: msdn
Upvotes: 1