Thread: How to add a multiply & division buttons???

  1. #1
    Registered User
    Join Date
    Nov 2010
    Location
    Philippines
    Posts
    2

    Unhappy How to add a multiply & division buttons???

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace calculator2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void txtDisplay_TextChanged(object sender, EventArgs e)
            {
    
            }
            double total1 = 0;
            double total2 = 0;
            double answer;
    
            private void btnOne_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnOne.Text;
            }
            private void btnTwo_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnTwo.Text;
            }
            private void btnThree_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnThree.Text;
            }
            private void btnFour_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnFour.Text;
            }
            private void btnFive_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnFive.Text;
            }
            private void btnSix_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnSix.Text;
            }
            private void btnSeven_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnSeven.Text;
            }
            private void btnEight_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnEight.Text;
            }
            private void btnNine_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnNine.Text;
            }
            private void btnZero_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnZero.Text;
            }
            private void btnClear_Click(object sender, EventArgs e)
            {
                txtDisplay.Clear();
            }
            private void btnPlus_Click(object sender, EventArgs e)
            {
                total1 = total1 + double.Parse(txtDisplay.Text);
                txtDisplay.Clear();
    
                plusButtonClicked = true;
                minusButtonClicked = false;
                divideButtonClicked = false;
                multiplyButtonClicked = false;
            }
            private void btnEquals_Click(object sender, EventArgs e)
            {
                if (plusButtonClicked == true)
                    
                {
                    total2 = total1 + double.Parse(txtDisplay.Text);
                   
                }
                if (multiplyButtonClicked == true)
                {
                    total2 = total1 * double.Parse(txtDisplay.Text);
                }
                else if (minusButtonClicked == true)
                {
                    total2 = total1 - double.Parse(txtDisplay.Text);
    
                }
                else if (divideButtonClicked == true)
                {
                    total2 = total1 / double.Parse(txtDisplay.Text);
                }
                txtDisplay.Text = total2.ToString();
                total1 = 0;
            }
            private void btnPoint_Click(object sender, EventArgs e)
            {
                txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
            }
            bool plusButtonClicked = false;
            bool minusButtonClicked = false;
            bool divideButtonClicked = false;
            bool multiplyButtonClicked = false;
            private void btnMinus_Click(object sender, EventArgs e)
            {
                total1 = total1 + double.Parse(txtDisplay.Text);
                txtDisplay.Clear();
    
                minusButtonClicked = true;
                plusButtonClicked = false;
                divideButtonClicked = false;
                multiplyButtonClicked = false;
            }
            
            private void btnMultiply_Click(object sender, EventArgs e)
            {
                total1 = total1 / double.Parse(txtDisplay.Text);
                multiplyButtonClicked = true;
                divideButtonClicked = true;
                minusButtonClicked = false;
                plusButtonClicked = false;
                txtDisplay.Clear();
            }
    
            private void btnDivide_Click(object sender, EventArgs e)
            {
                total1 = total1 / double.Parse(txtDisplay.Text);
                multiplyButtonClicked = true;
                divideButtonClicked = true;
                minusButtonClicked = false;
                plusButtonClicked = false;
                txtDisplay.Clear();
               
            }
        }
    }
    Last edited by Salem; 11-08-2010 at 11:44 AM. Reason: Added [code][/code] tags - learn to use them yourself

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This looks like it might be C#, so I have moved this thread to the C# programming forum.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Philippines
    Posts
    2

    Ok thanks for moving me in C Sharp!!

    And I runned this calculator program with only addition,subtraction,point,clear and equals code but without the multiplication and division code.

    And I did all the buttons in the form.Well I hope I can run this program by this two math operators multiplication and division and also for your help!!

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    First, read this: << !! Posting Code? Read this First !! >>

    Second, this is just the code-behind. The UI is all in the XAML.

    Third, what is your question? You've got event handlers for multiply and divide buttons.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    129
    All those number button events could be compressed to:

    Code:
    private void btnNumber_Click(object sender, EventArgs e)
    {
        txtDisplay.Text = txtDisplay.Text + ((button)sender).Text;
    }
    Assign all the number buttons to that click event.
    He who asks is a fool for five minutes, but he who does not ask remains a fool forever.

    The fool wonders, the wise man asks. - Benjamin Disraeli

    There are no foolish questions and no man becomes a fool until he has stopped asking questions. Charles Steinmetz

  6. #6
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i do not understand your question either.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Add ON_COMMAND menu button handler
    By kybert in forum Windows Programming
    Replies: 6
    Last Post: 09-15-2002, 07:33 PM
  2. Add and delete functions
    By Ana Val sazi in forum C++ Programming
    Replies: 5
    Last Post: 06-18-2002, 09:59 PM
  3. Cannot add file .c to C++ project
    By ooosawaddee3 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2002, 11:04 PM
  4. (Ken Fitlike) buttons
    By jdinger in forum C++ Programming
    Replies: 4
    Last Post: 03-15-2002, 01:21 PM
  5. Grouping radio buttons
    By Bazz in forum Windows Programming
    Replies: 1
    Last Post: 08-28-2001, 07:15 AM