Question
How can i allow functionality of my calculator program for the add/subtract/multiply/division and the enter keypress. See my code below. I highlighted area in bold
How can i allow functionality of my calculator program for the add/subtract/multiply/division and the enter keypress. See my code below. I highlighted area in bold as a refence to see if I am on the right track. Thank you
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 Calculator { public partial class Form1 : Form { private String calculatedNumber; // Stores numbers in string form private Boolean dot = false; // Falg: to control when a dot can be placed in a number public Form1() { InitializeComponent(); }
private void BtnZero_Click(object sender, EventArgs e) //adds 0 to string { calculatedNumber += "0"; LblDisplay.Text=calculatedNumber; }
private void BtnOne_Click(object sender, EventArgs e) //adds 1 to string { calculatedNumber += "1"; LblDisplay.Text = calculatedNumber; }
private void BtnTwo_Click(object sender, EventArgs e) //adds 2 to string { calculatedNumber += "2"; LblDisplay.Text = calculatedNumber; }
private void BtnThree_Click(object sender, EventArgs e) //adds 3 to string { calculatedNumber += "3"; LblDisplay.Text = calculatedNumber; }
private void BtnFour_Click(object sender, EventArgs e) //adds 4 to string { calculatedNumber += "4"; LblDisplay.Text = calculatedNumber; } private void button5_Click(object sender, EventArgs e) //adds 5 to string { calculatedNumber += "5"; LblDisplay.Text = calculatedNumber; } private void BtnSix_Click(object sender, EventArgs e) //adds 6 to string { calculatedNumber += "6"; LblDisplay.Text = calculatedNumber; }
private void BtnSeven_Click(object sender, EventArgs e) //adds 7 to string { calculatedNumber += "7"; LblDisplay.Text = calculatedNumber; }
private void BtnEight_Click(object sender, EventArgs e) //adds 8 to string { calculatedNumber += "8"; LblDisplay.Text = calculatedNumber; }
private void BtnNine_Click(object sender, EventArgs e) //adds 9 to string { calculatedNumber += "9"; LblDisplay.Text = calculatedNumber; }
private void BtnDot_Click(object sender, EventArgs e) //adds . to string { if (!dot) { calculatedNumber += "."; dot = true; }
LblDisplay.Text = calculatedNumber; }
private void BtnAdd_Click(object sender, EventArgs e) { //addition button }
private void BtnSub_Click(object sender, EventArgs e) { //subtraction button }
private void BtnDiv_Click(object sender, EventArgs e) { //divide button } private void button15_Click(object sender, EventArgs e) { //multiply button }
private void BtnEqt_Click(object sender, EventArgs e) { //equals button }
private void BtnClear_Click(object sender, EventArgs e) //resets the string to blank { calculatedNumber = ""; LblDisplay.Text = calculatedNumber; dot = false; }
private void BtnBack_Click(object sender, EventArgs e) { // reset dot if removed if (!LblDisplay.Text.Equals("")) { //if most right char is a . (dot) then reset flag if ((LblDisplay.Text.ToString())[LblDisplay.Text.Length-1] == '.') dot = false; // go ahead and remove last char LblDisplay.Text = calculatedNumber = LblDisplay.Text.Remove(LblDisplay.Text.Length - 1); } }
private void Form1_KeyPress(object sender, KeyPressEventArgs e) { if (Char.IsDigit(e.KeyChar))
{ LblDisplay.Text += e.KeyChar.ToString(); } else { e.Handled = true; }
}
private void Form1_KeyDown(object sender, KeyEventArgs e) { //LblDisplay.Text += e.KeyCode.ToString(); }
private void Form1_Load(object sender, EventArgs e) { this.KeyPreview = true; }
private void BTNAdd(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)Keys.Add) { LblDisplay.Text += e.KeyChar.ToString(); } else { e.Handled = true; } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started