In this article, i am going to make a simple calculator in C# using Microsoft Visual Studio.Let's start to make an application.
First open the Microsoft Visual Studio(no matter which version of Visual Studio you have) , and then click file menu at the top left side corner of Visual Studio and finally click new project.
When after clicking a new popup window will open which is shown below. Write the name of the project on the text after the name label and specify the location of project where you want to save it. And then Click OK button.
After Clicking button popup will disappear and it goes back to main windows, now the windows look like this.
Hover through View menu and click toolbox and properties respectively to open toolbox and properties windows and the screenshot will be look like the picture below.
Drag and drop one text box and 17 button from the toolbox on Form1 design window as like above screenshot and click on each button then after change text property of each button as shown above.
And double click on each button to write some code which is execute while clicking it.
First open the Microsoft Visual Studio(no matter which version of Visual Studio you have) , and then click file menu at the top left side corner of Visual Studio and finally click new project.
When after clicking a new popup window will open which is shown below. Write the name of the project on the text after the name label and specify the location of project where you want to save it. And then Click OK button.
After Clicking button popup will disappear and it goes back to main windows, now the windows look like this.
Hover through View menu and click toolbox and properties respectively to open toolbox and properties windows and the screenshot will be look like the picture below.
Drag and drop one text box and 17 button from the toolbox on Form1 design window as like above screenshot and click on each button then after change text property of each button as shown above.
And double click on each button to write some code which is execute while clicking it.
//A simple
Calculater Program
//Author
http://programerzone.blogspot.com
using System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
namespace
Kamal_Calculater
{
public partial class Form1 : Form
{
int
value1=0;
int
value2=0;
char
opt;
public
Form1()
{
InitializeComponent();
}
private
void button1_Click(object
sender, EventArgs e)
{
textBox1.Text+= "1";
}
private
void button2_Click(object
sender, EventArgs e)
{
textBox1.Text+= "2";
}
private
void button3_Click(object
sender, EventArgs e)
{
textBox1.Text+= "3";
}
private
void button4_Click(object
sender, EventArgs e)
{
textBox1.Text+= "4";
}
private
void button5_Click(object
sender, EventArgs e)
{
textBox1.Text+= "5";
}
private
void button6_Click(object
sender, EventArgs e)
{
textBox1.Text+= "6";
}
private
void button7_Click(object
sender, EventArgs e)
{
textBox1.Text+= "7";
}
private
void button8_Click(object
sender, EventArgs e)
{
textBox1.Text+= "8";
}
private
void button9_Click(object
sender, EventArgs e)
{
textBox1.Text+= "9";
}
private
void button10_Click(object
sender, EventArgs e)
{
textBox1.Text+= "0";
}
private
void button15_Click(object
sender, EventArgs e)
{
textBox1.Text+= ".";
}
private
void button17_Click(object
sender, EventArgs e)
{
textBox1.Clear();
textBox1.Focus();
}
private
void Form1_Load(object
sender, EventArgs e)
{
}
private
void button11_Click(object
sender, EventArgs e)
{
opt = '/';
value1= Int32.Parse(textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
}
private
void button12_Click(object
sender, EventArgs e)
{
opt = '*';
value1 = 1;
value1= Int32.Parse(textBox1.Text);
textBox1.Text = "";
textBox1.Focus();
}
private
void button13_Click(object
sender, EventArgs e)
{
opt = '+';
value1+= Int32.Parse(textBox1.Text);
textBox1.Clear();
textBox1.Focus();
}
private
void button14_Click(object
sender, EventArgs e)
{
opt = '-';
value1 =Int32.Parse(
textBox1.Text)-value1;
textBox1.Text = "";
textBox1.Focus();
}
private
void button16_Click(object
sender, EventArgs e)
{
int
result;
value2=Int32.Parse(textBox1.Text);
textBox1.Clear();
switch
(opt)
{
case
'/':
result = value1 / value2;
textBox1.Text =
result.ToString();
value1 = 0;
break;
case
'*':
result = value1 * value2;
textBox1.Text =
result.ToString();
value1 = 0;
break;
case
'+':
result = value1 + value2;
textBox1.Text =
result.ToString();
value1 = 0;
break;
case
'-':
result = value1 - value2;
textBox1.Text =
result.ToString();
value1 = 0;
break;
default:
textBox1.Text = "Error";
break;
}
}
}
}
Now the application is ready to run and press F5 to run it, The final screenshot is below.
Click here to download source code
Click here to download source code
Tags
C#.net