1. 1. Open Visual studio 2013.( or any other version)
2. On the menu bar, choose File, New, and Project.
Choose Programming language as Visual C#. In the templates list, choose the Windows Forms Application icon.
3.
Name the project as Calculator
and specify the address of your folder.( any other
name can be given it depends on you.)
4. In
the main window design the form as shown below and set the properties as
required.
In the above diagram, we have used one Textbox and named it as TxtResult. There are eighteen buttons used in this form for all the numbers from 0 to 9 , symbols for arithmetic operations like +, -, *, /. similarly one button is used for dot(.), one for equals to sign(=) , one for backspace. All these buttons are named accordingly using the properties. In each of the Click method write the Corresponding Code.
5. Write the following code for the button C. (clear)
private void btnClear_Click(object sender, EventArgs
e)
{
txtResult.Text = String.Empty;
}
6. Write the following code for the button -. (minus)
private
void btnMinus_Click(object
sender, EventArgs e)
{
op = '-';
fn = float.Parse(txtResult.Text);
txtResult.Text = String.Empty;
}
7. Write the following code for the button 4. (four)
private
void btn4_Click(object
sender, EventArgs e)
{
txtResult.Text += 4;
}
8. Write the following code for the button 5 (Five)
private
void btn5_Click(object
sender, EventArgs e)
{
txtResult.Text += 5;
}
9. Write the following code for the button 6. (Six)
private
void btn6_Click(object
sender, EventArgs e)
{
txtResult.Text += 6;
}
private
void btnPlus_Click(object
sender, EventArgs e)
{
op = '+';
fn = float.Parse(txtResult.Text);
txtResult.Text = String.Empty;
}
11. Write the following code for the button 1 (one)
private
void btn1_Click(object
sender, EventArgs e)
{
txtResult.Text += 1;
}
private
void btnDot_Click(object
sender, EventArgs e)
{
if (!txtResult.Text.Contains('.'))
txtResult.Text += ".";
}
13. Write the following code for the button <- .="" ackspace="" span="">->
private
void btnBackSpace_Click(object sender, EventArgs e)
{
if (txtResult.TextLength != 0)
{
txtResult.Text =
txtResult.Text.Remove(txtResult.Text.Length - 1, 1);
}
}
14. Write the following code for the button * . (Multiplication)
private
void btnMul_Click(object
sender, EventArgs e)
{
op = '*';
fn = float.Parse(txtResult.Text);
txtResult.Text = String.Empty;
}
15. Write the following code for the button /. (division)
private
void btnDiv_Click(object
sender, EventArgs e)
{
op = '/';
fn = float.Parse(txtResult.Text);
txtResult.Text = String.Empty;
}
16. Write the following code for the button 2. (Two)
private
void btn2_Click(object
sender, EventArgs e)
{
txtResult.Text += 2;
}
private
void btn3_Click(object
sender, EventArgs e)
{
txtResult.Text += 3;
}
18.Write the following code for the button 7. (seven)
private
void btn7_Click(object
sender, EventArgs e)
{
txtResult.Text += 7;
}
19. Write the following code for the button 8. (Eight)
private
void btn8_Click(object
sender, EventArgs e)
{
txtResult.Text += 8;
}
19. Write the following code for the button 9. (Nine)
private
void btn9_Click(object
sender, EventArgs e)
{
txtResult.Text += 9;
}
private
void btn0_Click(object
sender, EventArgs e)
{
txtResult.Text += 9;
}
21. Write the following code for the button equals to sign. (=)
private
void btnEqual_Click(object
sender, EventArgs e)
{
switch (op)
{
case '+':
sn = float.Parse(txtResult.Text);
result = fn +
sn;
txtResult.Text
= result.ToString();
break;
case '-':
sn = float.Parse(txtResult.Text);
result = fn -
sn;
txtResult.Text
= result.ToString();
break;
case '*':
sn = float.Parse(txtResult.Text);
result = fn *
sn;
txtResult.Text
= result.ToString();
break;
case '/':
sn = float.Parse(txtResult.Text);
result = fn /
sn;
txtResult.Text
= result.ToString();
break;
case 'c':
txtResult.Text
= String.Empty;
break;
}
22. Run your program and check the result
In this program, we have written different code for different numbers from 0 to 9. We can also write the same code for all these numbers provided we should use a switch case to execute a certain set of instructions meant for the number which is being pressed. in other words all the code written for individual numbers from 0 to 9 can be merged using a switch case and the final code can be copied in all these number buttons. Try it yourself. For further queries, please feel free to contact us.
Enjoy Programming...
Really nice ! Keep going sir !
ReplyDelete