In C# we can make a simple Calculator within an hour.simple limited
number of lines of code is needed for this. a sample design with
program(source code ) is here.addition,multiplication,subtraction and
division can be done using this calculator
In this the form have a TextBox for inputting and outputting the data change the name of the TextBox is changed to 'txtNumber' ,the form have 17 buttons for different uses.drag and drop a button to the form ,change the text property as '7' this button for print 7 on the TextBox,change the button controls name property as 'btn7'.like this add 9 button controls to form for 0-9 numbers also change the name property of all buttons like 'btn7'.now we have 5 buttons for arithmetic operation like '+','-','*, '=' and '/' so drag and drop 5 more buttons and change their names 'btnAdd','btnSub',btnMul','btnresult' and 'btndivision' respectively .finally we want 2 more buttons for clear and clear all operation in the above form these are represented by button 'C' and 'CA' respectively also changes the names of these buttons as 'btnClear' and 'btnClearAll'.now we are almost complete the design part of the form .after completing the design we can start the coding .for coding open the code page of the(double click the form) form and write the code here is the code for simple calculator:
Code :
In this the form have a TextBox for inputting and outputting the data change the name of the TextBox is changed to 'txtNumber' ,the form have 17 buttons for different uses.drag and drop a button to the form ,change the text property as '7' this button for print 7 on the TextBox,change the button controls name property as 'btn7'.like this add 9 button controls to form for 0-9 numbers also change the name property of all buttons like 'btn7'.now we have 5 buttons for arithmetic operation like '+','-','*, '=' and '/' so drag and drop 5 more buttons and change their names 'btnAdd','btnSub',btnMul','btnresult' and 'btndivision' respectively .finally we want 2 more buttons for clear and clear all operation in the above form these are represented by button 'C' and 'CA' respectively also changes the names of these buttons as 'btnClear' and 'btnClearAll'.now we are almost complete the design part of the form .after completing the design we can start the coding .for coding open the code page of the(double click the form) form and write the code here is the code for simple calculator:
Code :
public partial class Calculator : Form //'Calculator' is my forms name you just replace this with your //form name { decimal FirstValue; string Operation; public Calculator() { InitializeComponent(); txtNumber.Focus(); } private void Calculator_Load(object sender, EventArgs e) { } private void btnAdd_Click(object sender, EventArgs e) { FirstValue=Convert.ToDecimal (txtNumber.Text ); Operation = "ADD"; txtNumber.Clear (); txtNumber.Focus(); } private void btnEquals_Click(object sender, EventArgs e) { if (Operation == "ADD") { txtNumber.Text=((FirstValue)+(Convert.ToDecimal(txtNumber.Text ))).ToString(); } else if (Operation == "SUB") { txtNumber.Text = (FirstValue - Convert.ToDecimal(txtNumber.Text)).ToString(); } else if (Operation == "DIVIDE") { txtNumber.Text = (FirstValue / Convert.ToDecimal(txtNumber.Text)).ToString(); } else if (Operation == "MUL") { txtNumber.Text = (FirstValue * Convert.ToDecimal(txtNumber.Text)).ToString(); } } private void btnSub_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "SUB"; txtNumber.Clear(); txtNumber.Focus(); } private void btnDivide_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "DIVIDE"; txtNumber.Clear(); txtNumber.Focus(); } private void btnMul_Click(object sender, EventArgs e) { FirstValue = Convert.ToDecimal(txtNumber.Text); Operation = "MUL"; txtNumber.Clear(); txtNumber.Focus(); } private void btnClear_Click(object sender, EventArgs e) { txtNumber.Clear(); txtNumber.Focus(); } private void btnClearAll_Click(object sender, EventArgs e) { txtNumber.Clear(); FirstValue = 0; txtNumber.Focus(); } private void btn7_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 7; txtNumber.Focus(); int length= txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn8_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 8; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn9_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 9; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn4_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 4; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn5_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 5; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn6_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 6; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn3_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 3; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn2_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 2; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn1_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 1; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } private void btn0_Click(object sender, EventArgs e) { txtNumber.Text = txtNumber.Text + 0; txtNumber.Focus(); int length = txtNumber.Text.Length; txtNumber.Select(length, length); } } }
Tags:
csharp.net