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
}
}

New Topic/Question
Reply




MultiQuote







|