Click here to Skip to main content
16,012,352 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to add the values of textbox2 and textbox4 and the sum will show in textbox9 when I click the button1 total.

what code should I put in button1?

What I have tried:

C#
namespace LogIn
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Clear the Textboxes and the total labels/
            textBox2.Text="";
            textBox4.Text="";
            textBox9.Text = "";
            textBox1.Text="";
            textBox3.Text="";
            // reset the focus
            textBox9.Focus();
            textBox2.Focus();
            textBox4.Focus();
            textBox3.Focus();
            textBox1.Focus();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            DialogResult iLogOut;

            iLogOut = MessageBox.Show("Do you want to Log Out?", "Leave?", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (iLogOut == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
            {
                const double fries_small = 25;
                const double fries_large = 40;
                const double fries_bucket = 70;
                double friesQuantity;
                double friesTotal; // fries amount * cost
                friesQuantity = int.Parse(textBox1.Text);
                if (checkBox1.Checked == true)
                {
                    friesTotal = friesQuantity * fries_small;
                    textBox2.Text = friesTotal.ToString("c");
                }
                else if (checkBox2.Checked == true)
                {
                    friesTotal = friesQuantity * fries_large;
                    textBox2.Text = friesTotal.ToString("c");
                }
                else if (checkBox3.Checked == true)
                {
                    friesTotal = friesQuantity * fries_bucket;
                    textBox2.Text = friesTotal.ToString("c");
                }
                
                else
                {
                    MessageBox.Show("Fill the order form please");
                }
            }

            catch (Exception)
            {
                MessageBox.Show("Please fill out the order form");
            }
        }



        private void Form3_Load(object sender, EventArgs e)
        {
            MessageBox.Show("Original flavored fries and affordable drinks available, Order Now!");
        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
        const double bev_water = 20;
        const double bev_icedtea = 25;
        const double bev_coke = 30;
        double bevQuantity = int.Parse(textBox3.Text);
        double beveragesTotal; // beverage amount * cost
        
            if (checkBox4.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_water;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
                else if (checkBox5.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_icedtea;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
                else if (checkBox6.Checked == true)
                {
                    beveragesTotal = bevQuantity * bev_coke;
                    textBox4.Text = beveragesTotal.ToString("c");
                }
             else
                {
                    MessageBox.Show("Fill the order form please");
                }
            }
             catch(Exception)
            {
                MessageBox.Show("Please fill out the order form");
             }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        }

    }
Posted
Updated 19-Mar-17 19:54pm
Comments
[no name] 19-Mar-17 19:01pm    
You take the text from the textboxes, convert the strings to numbers, then use the + operator to add the numbers together and set the textbox text property to the sum.

Hi Sheena,

The code itself needs a lot of good reshaping.

The first thing you need to do is give a good name to the textboxes and buttons. Remember, a good name should sufficiently describe the actions they are up to; it reduces commenting efforts.

Suggestions E.g.:
button2 -> Rename as 'ResetBtn'
button4 -> Rename as 'ExitBtn'
button5 -> Rename as 'FriesOrderBtn'
button6 -> Rename as 'BeveragesOrderBtn'
button1 -> Rename as 'TotalPriceBtn'
checkBox1 -> Rename as 'SmallFriesCheckBox'
.....................
.....................
etc.
The following code makes unnecessary jumps between the textboxes:
// reset the focus
textBox9.Focus();
textBox2.Focus();
textBox4.Focus();
textBox3.Focus();
textBox1.Focus();
This can simply be just the last line; the first four lines are causing unnecessary trips between the controls.
textBox1.Focus();
Another thing you need to do is bring all constants at the top of the code; it looks unprofessional if you put them inside methods. Constants are created at compile time, not at runtime. It is not like they are created when you move to the method; they are created at the start of the program.

Another suggestion is, wrap your code in try-catch blocks. You never know what might happen in runtime.

Finally, the problem at your hand - adding the price of fries and beverages. You already used the solution (int.Parse(textBox1.Text)), you can use the same.
private void button1_Click(object sender, EventArgs e)
{
	// Check if prices for fries and beverages are calculated yet. If not then return.
	if (string.IsNullOrEmpty(textBox2.Text) || string.IsNullOrEmpty(textBox4.Text))
        return;

	try
    {
        int Total = int.Parse(textBox2.Text) + 	int.Parse(textBox4.Text);
	    textBox9.Text = Total.ToString();
    }
    catch (FormatException Ex)
    {
        MessageBox.Show("Expecting numbers in the price boxes of fries and beverages.
                         Error msg: " + Ex.Message);
    }
}
 
Share this answer
 
Comments
Animesh Datta 20-Mar-17 0:44am    
5 for explanation.
1. convert the currency string into number
2. sum the two number
3. convert it back into currency

C#
double beveragesTotal, friesTotal;

double.TryParse(textBox4.Text, System.Globalization.NumberStyles.Currency, 
System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out beveragesTotal);

double.TryParse(textBox2.Text, System.Globalization.NumberStyles.Currency, 
System.Globalization.CultureInfo.CurrentCulture.NumberFormat, out friesTotal);

textBox9.Text = (beveragesTotal + friesTotal).ToString("c");
 
Share this answer
 
First Convert textbox value into numeric value(like int,double.)
Ex- int val1 = Convert.ToInt32(TextBox2.Text);
int val2 = Convert.ToInt32(TextBox2.Text);

Second Add those value...using a temp variable
Ex- int result = val1+val2;

Third Assign this result into TextBox
Ex- TextBox3.Text= result.ToString();

Or
In Only One Line

TextBox3.Text = (Convert.ToInt32(TextBox2.Text) + Convert.ToInt32(TextBox2.Text));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900