I have to clone the calc.exe from winxp and i'm stuck.I'm new in c#.
The problem is that I don't know how to get the next number when i press " + ",I take the first number from the textbox but after that I don't know how to read the next number so I can sum them.I think I'll have to do this in the "=" button somehow...but I don't really know how

Here is the code that i wrote so far

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {//Buton pt cifre(inclusiv 0);
            if (textBox1.Text.Equals("0"))
                textBox1.Text = ((Button)sender).Text;
            else
                textBox1.Text = textBox1.Text+((Button)sender).Text;

        }

        private void button14_Click(object sender, EventArgs e)
        {//Buton pt +/-;
            String S = textBox1.Text;
            textBox1.Text=null;
            if (S[0].Equals('-'))
                for (int i = 1; i < S.Length; i++)
                    textBox1.Text = textBox1.Text + S[i];
            else
                textBox1.Text = '-'+S;
        }

        private void button15_Click(object sender, EventArgs e)
        {//Buton pt . ;
            String S = textBox1.Text;
            Boolean x = true;
            for (int i = 0; i < S.Length - 1; i++)
                if (S[i].Equals('.'))
                    x = false;
            if (x)
                textBox1.Text = S + '.';
        }

        private void button20_Click(object sender, EventArgs e)
        {

        }

        private void button21_Click(object sender, EventArgs e)
        {//Buton C ;
            textBox1.Text = "0";
        }

        private void button23_Click(object sender, EventArgs e)
        {//Buton Backspace;
            String S = textBox1.Text;
            textBox1.Text = null;
            if (S.Length == 1)
                textBox1.Text = "0";
            else
                for (int i = 0; i < S.Length-1; i++)
                textBox1.Text = textBox1.Text+S[i];

            

        }

        private void button16_Click(object sender, EventArgs e)
        {//Buton +;
            int a=int.Parse(textBox1.Text);
            
        }
    }
}

Recommended Answers

All 8 Replies

Hi forhacksf, welcome at daniweb!
Have a look at this thread: http://www.daniweb.com/forums/thread201807.html. It is not about your problem but it contains a zip file somewhere of a calculator in C# that might give you some ideas. It is a "clone" of a little pocket calculator I have at home not a "clone" of the windows one.

i am beginner of c# please tell me from where i start?

Hi hunde, welcome at daniweb!
Is this a question about this calc thread?
Your question is rather general, if you have a specific question please post it in a different thread.

First off, i'm impressed that as a new coder you used a single button for the numbers 0-9. Most beginners would have had 10 different buttons for that, you are already thinkin like a coder :)

If you want to mimic the windows calc then when the user presses a function button (+/-/*/etc) the first time you should store the contents of the textbox and store which function they selected then reset the textbox to 0 ready for next number.
When they click '=' or a second function button you should apply the stored function against the number you have stored and the number in the textbox.

If they clicked '=' then just display the result. If they clicked a second function button then you need to display the result AND store it in place of the first number and update the stored function.

ty,very helpful.I'm allready going on 1 ideea and i will post when I'm done.This is a homework from college and I had a little help with the button sender

Aah, that explains it :p In that case, take note of how the button sender was written, you should always be looking to make your code more streamlined and efficient...why have 10 seperate button click events when they all do the same job. Any time you are given code be sure to study how it works and figure out why so that you learn from it.

I think I am on the right path :)
I can do operations like 5+3-41*34/2 now :D
I made all the basic operations in one button.(no help)

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;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Boolean ok=false;
        Double var2 = 0;
        String operation = null;
        public Form1()
        {

            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {//Buton pt cifre(inclusiv 0);
            if (ok)
                textBox1.Text = null;
            
            if (textBox1.Text.Equals("0"))

                textBox1.Text = ((Button)sender).Text;
            else
                textBox1.Text = textBox1.Text+((Button)sender).Text;
            ok = false;

        }

        private void button14_Click(object sender, EventArgs e)
        {//Buton pt +/-;
            String S = textBox1.Text;
            textBox1.Text=null;
            if (S[0].Equals('-'))
                for (int i = 1; i < S.Length; i++)
                    textBox1.Text = textBox1.Text + S[i];
            else
                textBox1.Text = '-'+S;
        }

        private void button15_Click(object sender, EventArgs e)
        {//Buton pt . ;
            String S = textBox1.Text;
            Boolean x = true;
            for (int i = 0; i < S.Length - 1; i++)
                if (S[i].Equals('.'))
                    x = false;
            if (x)
                textBox1.Text = S + '.';
        }

        private void button20_Click(object sender, EventArgs e)
        {//Buton 1/x ;

        }

        private void button21_Click(object sender, EventArgs e)
        {//Buton C ;
            var2 = 0;
            ok = false;
            operation = null;
            textBox1.Text = "0";
        }

        private void button23_Click(object sender, EventArgs e)
        {//Buton Backspace ;
            String S = textBox1.Text;
            textBox1.Text = null;
            if (S.Length == 1)
                textBox1.Text = "0";
            else
                for (int i = 0; i < S.Length-1; i++)
                textBox1.Text = textBox1.Text+S[i];

            

        }

        private void button16_Click(object sender, EventArgs e)
        {//Buton  (+,-,*,/);
            Double var1;
            if (operation!=null)
                switch (operation)
                {
                    case "+":
                        var1 = Convert.ToDouble(textBox1.Text);
                        ok = true;
                        var2 = var2 + var1;
                        textBox1.Text = var2.ToString();
                        break;
                    case "-":
                        var1 = Convert.ToDouble(textBox1.Text);
                        ok = true;
                        var2 = var2 - var1;
                        textBox1.Text = var2.ToString();
                        break;
                    case "*":
                        var1 = Convert.ToDouble(textBox1.Text);
                        ok = true;
                        var2 = var1 * var2;
                        textBox1.Text = var2.ToString();
                        break;
                    case "/":
                        var1 = Convert.ToDouble(textBox1.Text);
                        ok = true;
                        var2 = var2 / var1;
                        textBox1.Text = var2.ToString();
                        break;
                }
            operation=((Button)sender).Text;
            switch (operation)
            {
                case "+":
                    var2 = Convert.ToDouble(textBox1.Text);
                    ok = true;
                    operation = "+";
                    break;
                case "-":
                    var2 = Convert.ToDouble(textBox1.Text);
                    ok = true;
                    operation = "-";
                    break;
                case "*":
                    var2 = Convert.ToDouble(textBox1.Text);
                    ok = true;
                    operation = "*";
                    break;
                case "/":
                    var2 = Convert.ToDouble(textBox1.Text);
                    ok = true;
                    operation = "/";
                    break;
            }
                 
        }

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

        private void button17_Click(object sender, EventArgs e)
        {//buton = ;
            Double var1=0;
            switch (operation)
            {
                case "+":
                    var1 = Convert.ToDouble(textBox1.Text);
                    Double sum = var1 + var2;
                    textBox1.Text=sum.ToString();
                    break;
                case "-":
                    var1 = Convert.ToDouble(textBox1.Text);
                    Double dif = var2 - var1;
                    textBox1.Text = dif.ToString();
                    break;
                case "*":
                    var1=Convert.ToDouble(textBox1.Text);
                    Double inm = var1 * var2;
                    textBox1.Text = inm.ToString();
                    break;
                case "/":
                    var1 = Convert.ToDouble(textBox1.Text);
                    Double imp = var2 / var1;
                    textBox1.Text = imp.ToString();
                    break;
                default:
                    break;
            }
          
        }

        private void fileToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void button22_Click(object sender, EventArgs e)
        {//buton CE;
            textBox1.Text = "0";
        }

        private void button18_Click(object sender, EventArgs e)
        {//buton sqrt ;
            Double var1 = Convert.ToDouble(textBox1.Text);
            var2=Math.Sqrt(var1);
            textBox1.Text = var2.ToString();

        }

        private void button19_Click(object sender, EventArgs e)
        {//buton % ;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            if (FormWindowState.Minimized == WindowState)
                Hide();

        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;

        }
    }
}

Looking good :)
One minor note, whenever you are converting input from a user its best to use double.TryParse() rather than Convert.ToDouble. The Convert operation will throw an exception if the string typed in isnt a valid number. TryParse allows you to catch incorrect input without an exception. It accepts a string as an input, a variable as an output and returns a boolean value to indicate wether the parse was successful:

double num;
bool success;
success = double.TryParse(TextBox1.Text, out num); //out keyword creates reference to num variable

if(success)
{    
    //process valid input
    //num contains converted value
}
else
{
    //handle invalid input
    //num = 0
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.