4 Replies - 15051 Views - Last Post: 05 December 2008 - 06:26 PM Rate Topic: -----

#1 sonbol_montakhab  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-December 08

creating calculator with c#

Posted 03 December 2008 - 10:54 AM

Hi,
I would like to create a calculator using visual studio (win app)C#, I've put 16 buttons on the form and this is my code so far:
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 WindowsFormsApplication6
{
	public partial class Form1 : Form
	{
		public int a, b, c;
		public Form1()
		{
			InitializeComponent();
			
		}

	  
		private void button13_Click(object sender, EventArgs e)
		{
			textBox1.Text = "0";
		}

		private void button9_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button9.Text;
		}

		private void button10_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button10.Text;
		}

		private void button11_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button11.Text;
		}

		private void button5_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button5.Text;
		}

		private void button6_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button6.Text;
		}

		private void button7_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button7.Text;
		}

		private void button1_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button1.Text;
		}

		private void button2_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button2.Text;
		}

		private void button3_Click(object sender, EventArgs e)
		{
			if (textBox1.Text == "0")
				textBox1.Text = "";
			textBox1.Text = textBox1.Text + button3.Text;
		}



now I want to add the two numbers that I push on the caclculater , I want to know how can I cast (convert) type string to int , I wrote this code but it didn't work :
 private void button16_Click(object sender, EventArgs e)
		{
			a = (int)button1.Text;
			b = (int)button2.Text;
			c = a + b;
		}

I just need some help to complete the add section on the calculator ,
Best Regards
}
}

Is This A Good Question/Topic? 1
  • +

Replies To: creating calculator with c#

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9201
  • View blog
  • Posts: 34,574
  • Joined: 12-June 08

Re: creating calculator with c#

Posted 03 December 2008 - 02:44 PM

ah.. nm

This post has been edited by modi123_1: 03 December 2008 - 03:29 PM

Was This Post Helpful? 0
  • +
  • -

#3 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: creating calculator with c#

Posted 03 December 2008 - 02:50 PM

Hi
You can convert a string to an int using either int.Parse(string) or int.TryParse(string,out int) methods of int type.
both of these method do the same thing using two different methods.
int.Parse gets a string and returns an int value, however if the string can't be parsed then an exception will be thrown.
The int.TryParse method gets a string and an out argument as int and returns a boolean value that will be true if the operation is successful and you can get the value using the out argument. It won't throw any exception so it's safer to use this method instead of int.Parse
Was This Post Helpful? 1

#4 sssmartie  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 03-June 08

Re: creating calculator with c#

Posted 03 December 2008 - 03:00 PM

You can also use the following:

a = Convert.Int(textBox1.Text);

b = Convert.ToInt(textBox1.Text);

Woops!Please correct the first line mistake!:


*a = Convert.ToInt(textBox1.Text);
Was This Post Helpful? 0
  • +
  • -

#5 gbertoli3  Icon User is offline

  • DIC at Heart + Code
  • member icon

Reputation: 40
  • View blog
  • Posts: 1,162
  • Joined: 23-June 08

Re: creating calculator with c#

Posted 05 December 2008 - 06:26 PM

Here is PsychoCoder's Tutorial Basic Calculator in C#

Hope this helps
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1