3 Replies - 312 Views - Last Post: 24 September 2012 - 12:16 AM Rate Topic: -----

#1 Gmichael  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 05-September 12

Calculator Creation wont accept number by key board

Posted 23 September 2012 - 03:13 PM

Hello,

I have created this calculator for my school assignment. I cant seem to find out why my calculator accepts for the number to be pressed by button on the form multiple times, but if I press for example the number 1 repeatedly on my keyboard it wont allow it to show up more then once and then that button wont work again. Please help I have posted my code below

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GMassign2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btnOne.Enabled = false;
            btnTwo.Enabled = false;
            btnThree.Enabled = false;
            btnFour.Enabled = false;
            btnFive.Enabled = false;
            btnSix.Enabled = false;
            btnSeven.Enabled = false;
            btnEight.Enabled = false;
            btnNine.Enabled = false;
            btnZero.Enabled = false;
            btnClear.Enabled = false;
            btnAdd.Enabled = false;
            btnSub.Enabled = false;
            btnMult.Enabled = false;
            btnDiv.Enabled = false;
            btnEquals.Enabled = false;
        }
        
        double total1 = 0;
        double total2 = 0;
        bool addButtonclicked = false;
        bool subButtonclicked = false;
        bool multButtonclicked = false;
        bool divButtonclicked = false;
        bool formVisiableClicked = false;

           

        private void btnOff_Click(object sender, EventArgs e)
        {
            txtNumeric.Text = null;

            btnOne.Enabled = false;
            btnTwo.Enabled = false;
            btnThree.Enabled = false;
            btnFour.Enabled = false;
            btnFive.Enabled = false;
            btnSix.Enabled = false;
            btnSeven.Enabled = false;
            btnEight.Enabled = false;
            btnNine.Enabled = false;
            btnZero.Enabled = false;
            btnClear.Enabled = false;
            btnAdd.Enabled = false;
            btnSub.Enabled = false;
            btnMult.Enabled = false;
            btnDiv.Enabled = false;
            btnEquals.Enabled = false;

        }

        private void btnOn_Click(object sender, EventArgs e)
        {

            btnOne.Enabled = true;
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;
            btnClear.Enabled = true;
            btnAdd.Enabled = true;
            btnSub.Enabled = true;
            btnMult.Enabled = true;
            btnDiv.Enabled = true;
            btnEquals.Enabled = true;
        
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOne_Click(object sender, EventArgs e)
        {
            
            btnOne.Text = "1";
            txtNumeric.Text = txtNumeric.Text + btnOne.Text;
        }

        private void btnTwo_Click(object sender, EventArgs e)
        {
            btnTwo.Text = "2";
            txtNumeric.Text = txtNumeric.Text + btnTwo.Text;
        }

        private void btnThree_Click(object sender, EventArgs e)
        {
            btnThree.Text = "3";
            txtNumeric.Text = txtNumeric.Text + btnThree.Text;
        }

        private void btnFour_Click(object sender, EventArgs e)
        {
            btnFour.Text = "4";
            txtNumeric.Text = txtNumeric.Text + btnFour.Text;
        }

        private void btnFive_Click(object sender, EventArgs e)
        {
            btnFive.Text = "5";
            txtNumeric.Text = txtNumeric.Text + btnFive.Text;
        }

        private void btnSix_Click(object sender, EventArgs e)
        {
            btnSix.Text = "6";
            txtNumeric.Text = txtNumeric.Text + btnSix.Text;
        }

        private void btnSeven_Click(object sender, EventArgs e)
        {
            btnSeven.Text = "7";
            txtNumeric.Text = txtNumeric.Text + btnSeven.Text;
        }

        private void btnEight_Click(object sender, EventArgs e)
        {
            btnEight.Text = "8";
            txtNumeric.Text = txtNumeric.Text + btnEight.Text;
        }

        private void btnNine_Click(object sender, EventArgs e)
        {
            btnNine.Text = "9";
            txtNumeric.Text = txtNumeric.Text + btnNine.Text;
        }

        private void btnZero_Click(object sender, EventArgs e)
        {
            btnZero.Text = "0";
            txtNumeric.Text = txtNumeric.Text + btnZero.Text;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            btnOne.Enabled = true;
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;
            txtNumeric.ReadOnly = false;
            txtNumeric.Clear();
          
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            addButtonclicked = false;
            subButtonclicked = true;
            multButtonclicked = false;
            divButtonclicked = false;

            btnOne.Enabled = true;
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;

            total1 = total1 + double.Parse(txtNumeric.Text);
            txtNumeric.Clear();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            addButtonclicked = true;
            subButtonclicked = false;
            multButtonclicked = false;
            divButtonclicked = false;
            btnOne.Enabled = true;
           
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;
            
            total1 = total1 + double.Parse(txtNumeric.Text);
            txtNumeric.Clear();
        }

        private void btnDiv_Click(object sender, EventArgs e)
        {
            addButtonclicked = false;
            subButtonclicked = false;
            multButtonclicked = false;
            divButtonclicked = true;

            btnOne.Enabled = true;
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;

            total1 = total1 + double.Parse(txtNumeric.Text);
            txtNumeric.Clear();
        }

        private void btnMult_Click(object sender, EventArgs e)
        {
            addButtonclicked = false;
            subButtonclicked = false;
            multButtonclicked = true;
            divButtonclicked = false;

            btnOne.Enabled = true;
            btnTwo.Enabled = true;
            btnThree.Enabled = true;
            btnFour.Enabled = true;
            btnFive.Enabled = true;
            btnSix.Enabled = true;
            btnSeven.Enabled = true;
            btnEight.Enabled = true;
            btnNine.Enabled = true;
            btnZero.Enabled = true;

            total1 = total1 + double.Parse(txtNumeric.Text);
            txtNumeric.Clear();
        }

        private void btnEquals_Click(object sender, EventArgs e)
        {
            
            btnOne.Enabled = false;
            btnTwo.Enabled = false;
            btnThree.Enabled = false;
            btnFour.Enabled = false;
            btnFive.Enabled = false;
            btnSix.Enabled = false;
            btnSeven.Enabled = false;
            btnEight.Enabled = false;
            btnNine.Enabled = false;
            btnZero.Enabled = false;

            txtNumeric.ReadOnly = true;
            if (addButtonclicked == true)
            {
                total2 = total1 + double.Parse(txtNumeric.Text);
                
                
            }
            else if (subButtonclicked == true)
            {
                total2 = total1 - double.Parse(txtNumeric.Text);

            }
            else if (multButtonclicked == true)
            {
                total2 = total1 * double.Parse(txtNumeric.Text);
            }
            else if (divButtonclicked == true)
            {
                total2 = total1 / double.Parse(txtNumeric.Text);
            }

            txtNumeric.Text = total2.ToString();
            total1 = 0;
        }

        void NumberValidation(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 48 && e.KeyChar <= 57) || e.KeyChar == 46)
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Calculator Creation wont accept number by key board

#2 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,421
  • Joined: 08-April 09

Re: Calculator Creation wont accept number by key board

Posted 23 September 2012 - 03:44 PM

Wow lots of repeating :)
And here Basic Calculator
Was This Post Helpful? 2
  • +
  • -

#3 Gmichael  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 05-September 12

Re: Calculator Creation wont accept number by key board

Posted 23 September 2012 - 03:52 PM

yeah I haven't really learned how to create and call functions yet, So i can keep my code a little cleaner. Im sure eventually it will look nicer. Ive only been coding for 3 weeks. :bigsmile: Thanks for the link I will try to find my answer there.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • Radar love
  • member icon

Reputation: 332
  • View blog
  • Posts: 1,421
  • Joined: 08-April 09

Re: Calculator Creation wont accept number by key board

Posted 24 September 2012 - 12:16 AM

The C# Station Tutorial: Methods
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1