Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make it so if a person clicks equals to twice or after pressing any button it does (textbox1.clear & labelbox1 = "";)

What I have tried:

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 Ccalculator
{
    public partial class Form1 : Form
    {
        Decimal ResultValue = 0;
        String operatorperformed = "";
        bool IsOperatorPerformed = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void button_Click(object sender, EventArgs e)
        {
            if(textBox1.Text == "0"||IsOperatorPerformed)
            {
                textBox1.Clear();
            }

            Button button = (Button)sender;
            if (textBox1.Text == ".")
            {
                if (!textBox1.Text.Contains("."))
                {
                    textBox1.Text = textBox1.Text + button.Text;
                }
            }else
            textBox1.Text = textBox1.Text + button.Text;
        }

        private void buttonBackSpace_Click(object sender, EventArgs e)
        {
            int i = textBox1.Text.Length;
            textBox1.Text = textBox1.Text.Substring(0, i - 1);
        }

        private void Button_Operator(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            if (ResultValue != 0)
            {
                buttonEqualsTo.PerformClick();
                operatorperformed = button.Text;
                label1.Text = ResultValue + " " +
                    operatorperformed;
                IsOperatorPerformed = true;
            }
            else
            {
                operatorperformed = button.Text;
                ResultValue = Decimal.Parse(textBox1.Text);
                label1.Text = ResultValue + " " + operatorperformed;
                IsOperatorPerformed = true;
            }
        }

        private void buttonEqualsTo_Click(object sender, EventArgs e)
        {
            switch (operatorperformed)
            {
                case "+":
                    textBox1.Text = (ResultValue + Decimal.Parse
                        (textBox1.Text)).ToString();
                    break;
                case "-":
                    textBox1.Text = (ResultValue - Decimal.Parse
                        (textBox1.Text)).ToString();
                    break;
                case "/":
                    textBox1.Text = (ResultValue / Decimal.Parse
                        (textBox1.Text)).ToString();
                    break;
                case "x":
                    textBox1.Text = (ResultValue * Decimal.Parse
                        (textBox1.Text)).ToString();
                    break;
            }
            ResultValue = Decimal.Parse(textBox1.Text);
            label1.Text = "";
        }

        private void buttonC_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
            label1.Text = "";
        }

        private void butCE_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
    }
}
Posted
Updated 27-Jul-18 1:21am
Comments
Richard MacCutchan 27-Jul-18 7:19am    
OK, that is what you want to happen. What is the problem?
NotAComputerScienceStudent 27-Jul-18 10:03am    
The problem is that pressing equals to it multiplies the number over and over again when i press equals to and if i press any numerical button it just adds to it (50 becomes 506) i want it to reset
Richard MacCutchan 27-Jul-18 10:10am    
See OriginalGriff's answer below.

1 solution

It's not difficult: create a class level variable - a bool is fine - which says "data modified".
When any text box changes, set it to true if the content of all textboxes if not empty..
When ENTER is pressed, your Click handler checks the variable and adds if it's set, and clears if it isn't.
 
Share this answer
 
Comments
NotAComputerScienceStudent 27-Jul-18 13:34pm    
could you like give an example or explain a tad bit better because i donot understand what you just said(Yes i am a noob).
Like i am making the standard windows 10 calculator and am done with everything but this
OriginalGriff 27-Jul-18 13:58pm    
Oh come on! Even as a "noob" you should understand that lot. Even VB developers can do it...
I'll give you one line of code to get you started.
private bool dataModified = false;
Now, how do you find out if a text box changed?
NotAComputerScienceStudent 27-Jul-18 16:29pm    
I know this how should I find out whether a textbox has changed
OriginalGriff 27-Jul-18 17:10pm    
How do you think?
Hint: what events does the TextBox support? One of the names might be really useful...
NotAComputerScienceStudent 28-Jul-18 0:30am    
textChanged property in events

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