Question: C# Windows Form Application (CALCULATOR): (This is the instruction below): Windows Calculator that will going to create a Windows Form Application that will mimics a
C# Windows Form Application (CALCULATOR):
(This is the instruction below):
Windows Calculator that will going to create a Windows Form Application that will mimics a calculator. This is the screenshot how it shouldl look like. Calculator It should have a textbox and the following buttons: + Addition - Subtraction * Multiplication / Division = Equals (Will perform the final calculation) C Clear (Will clear the text box) There will be no maximize or minimize buttons. The rules are these: 2 A user enters a number (in the app youll use the datatype Decimal) and they will then click an operation button. Then they can enter another number and if they press equals then youre done and the result will be displayed. No other inputting of numbers is allowed. If they dont press equals and instead click another operation, then clear the textbox and allow them to enter another number. They can so this as many times as they like. Once the equals button is pressed make the text box read only and disable all of the buttons (Except for C) until C (clear) is clicked and then re-enable the buttons, clear and enable the text box and allow them to start again. So, if they enter 30, then click -, then enter 5, and then press = (equals) then 25 will be displayed in the textbox. The textbox will be read-only and all of the buttons except for the C (clear) button will be disabled. Once they click C (Clear) all of the buttons will be enabled, the textbox will be enabled, and cleared. Then they can start again. If they enter 30, and the (Minus), and then 5, and then * (Multiply), then 5, and then = (equals) then 125 will be displayed in the box that is read-only, and all of the buttons except for the C button will be disabled. In this application youll be coding for bad input. So if they type in anything but a valid decimal youre going to alert them (Use a messagebox) that their input was invalid. Allow them to enter new input. Of course this will happen on the operation click (Either +, -, *, /, or =). Also, youll have to code for division by zero. So, if they type in a number, say 5, then / and then 0, then youll message box them to say 3 they cannot divide by zero. Then allow them to re-enter their input. Use Decimal.TryParse to check all numbers for validity.
this is my code: (there are a error and couldn't fix it so please help me)
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;
namespace Project05 { public partial class Form1 : Form
{ Decimal num1, num2, ans; String op; int count; bool div = false; Decimal number;
private bool TestValue(String numTest) { bool test = decimal.TryParse(numTest, out number); if (test == false) { MessageBox.Show(" You goofed up something"); } textBox1.Clear(); textBox1.Focus(); return test; }
public Form1() { InitializeComponent(); }
private void button3_Click(object sender, EventArgs e)//Add button {
//decimal number; div = false; if (TestValue(textBox1.Text)) { num1 = number; } count = 2; }
private void textBox1_TextChanged(object sender, EventArgs e) {
}
private void button2_Click(object sender, EventArgs e)//subtract Button { div = false; if (textBox1.Text != "") { //decimal number; if (TestValue(textBox1.Text)) { num1 = number; } count = 1; } }
private void button4_Click(object sender, EventArgs e)//Mutiply Button { div = false; if (TestValue(textBox1.Text)) {
num1 = number; } count = 3;
}
private void button5_Click(object sender, EventArgs e)//divide Button { div = true;
if (TestValue(textBox1.Text)) { num1 = number; } count = 4;
}
private void button6_Click(object sender, EventArgs e)//equal { div = false; compute(count); button1.Enabled = true;
}
private void button1_Click(object sender, EventArgs e)//Clear button { div = false; textBox1.Clear(); textBox1.Focus(); count = 0; } public void compute(int count) { switch (count) { case 1: ans = num1 - float.Parse(textBox1.Text); textBox1.Text = ans.ToString(); break; case 2: ans = num1 + float.Parse(textBox1.Text); textBox1.Text = ans.ToString(); break; case 3: ans = num1 * float.Parse(textBox1.Text); textBox1.Text = ans.ToString(); break; case 4: if (float.Parse(textBox1.Text) == 0) { MessageBox.Show(" You Can Not Divide by 0!!"); } else { ans = num1 / float.Parse(textBox1.Text); } textBox1.Text = ans.ToString(); break; default: break; }
} } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
