Hi I am struggling to keep a value in my calculator's text box when I add a decimal point to a number and then want to add another number after the decimal point. Here is my code: What code must I add to keep the value in the text box after I added a decimal point? Thank you!
public Form1()
{
InitializeComponent();
}
double total1 = 0;
double total2 = 0;
bool plusButtonClicked = false;
bool subtractButtonClicked = false;
bool multiplyButtonClicked = false;
bool divideButtonClicked = false;
private void btnSeven_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnSeven.Text;
}
private void btnNine_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnNine.Text;
}
private void btnEquals_Click(object sender, EventArgs e)
{
if (plusButtonClicked == true)
{
total2 = total1 + double.Parse(txtDisplay.Text);
}
else if (subtractButtonClicked == true)
{
total2 = total1 - double.Parse(txtDisplay.Text);
}
else if (multiplyButtonClicked == true)
{
total2 = total1 * double.Parse(txtDisplay.Text);
}
else if (divideButtonClicked == true)
{
total2 = total1 / double.Parse(txtDisplay.Text);
}
{
txtDisplay.Text = total2.ToString();
total1 = 0;
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = true;
subtractButtonClicked = false;
multiplyButtonClicked = false;
divideButtonClicked = false;
}
private void btnOne_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnOne.Text;
}
private void btnTwo_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnTwo.Text;
}
private void btnThree_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnThree.Text;
}
private void btnFour_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnFour.Text;
}
private void btnFive_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnFive.Text;
}
private void btnSix_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnSix.Text;
}
private void btnEight_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnEight.Text;
}
private void btnPoint_Click(object sender, EventArgs e)
{
txtDisplay.Text = txtDisplay.Text + btnPoint.Text;
}
private void btnZero_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnZero.Text;
}
private void btnSubtract_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
subtractButtonClicked = true;
multiplyButtonClicked = false;
divideButtonClicked = false;
}
private void btnMultiply_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
subtractButtonClicked = false;
multiplyButtonClicked = true;
divideButtonClicked = false;
}
private void btnDivide_Click(object sender, EventArgs e)
{
total1 = total1 + double.Parse(txtDisplay.Text);
txtDisplay.Clear();
plusButtonClicked = false;
subtractButtonClicked = false;
multiplyButtonClicked = false;
divideButtonClicked = true;
}
private void btnClear_Click(object sender, EventArgs e)
{
txtDisplay.Clear();
}
}
}
The issue seems to be that you are doing this:
private void some_button_Click(object sender, EventArgs e)
{
txtDisplay.Text = btnEight.Text;
}
So, if you click 4
, and then .
it'll show 4.
, but when you click 5
, it'll set it to 5
.
You need to add to the previous on all your numbers ... (just like you do with the .
button)
As Noctis said you need to add to the previous in all your click events. Like :
private void some_button_Click(object sender, EventArgs e)
{
txtDisplay.Text += ((Button)sender).Text;
}
the answer is simple
txtDisplay.Text += Button.Text;
Improve your code
public class Form1:Form
{
public enum CalcStatus
{
None,
Plus,
Minus,
Divide,
Multiply
};
CalcStatus status = CalcStatus.None;
bool bHasNewFlag = false;
bool bHasEquals = false;
Decimal dMemory1 = 0M;
Decimal dMemory2 = 0M;
private void CalcButton_Click(object sender, EventArgs e) // This event will be handled by Plus,Minus,Devide and Multiply
{
Decimal.TryParse(txtDisplay.Text,out dMemory1);
switch(((button)sender).Text)
{
case "+":
status = CalcStatus.Plus;
break;
case "-":
status = CalcStatus.Minus;
break;
case "/":
status = CalcStatus.Divide;
break;
case "*":
status = CalcStatus.Multiply;
break;
}
bHasNewFlag = true;
bHasEquals =false;
dMemroy2 = 0M;
}
private void NumButton_Click(object sender, EventArgs e) // This event will be handled by numbers (0-9)
{
int iNumber = 0;
int.TryParse(((button)sender).Text, out iNumber);
if (bHasNewFlag)
txtDisplay.Text = string.empty;
txtDisplay.Text += iNumber.ToString();
bHasEquals =false;
dMemroy2 = 0M;
}
private void EqualsTo_Click(object sender, EventArgs e) // This event will be handled by Equals To (=)
{
Decimal dVal = 0M;
if (bHasEquals == false)
Decimal.TryParse(txtDisplay.Text, out dMemory1);
switch(status)
{
case "+":
dVal = dMemory1 + dMemory2;
break;
case "-":
status = CalcStatus.Minus;
dVal = dMemory1 - dMemory2;
break;
case "/":
dVal = dMemory1 / dMemory2;
break;
case "*":
dVal = dMemory1 * dMemory2;
break;
}
txtDisplay.Text = dVal;
bHasEquals =true;
}
}
Do not take boolean flag for each button. because, you can do one thing at one time.
Updates: My suggestion is when you have status like Plus, Minus, Divide, etc. then you can do any one thing at a time. for example here you have to manage 4 variables on each button click instead of one. so use Enumaration when you have single status value.
public enum CalcStatus
{
None,
Plus,
Minus,
Divide,
Multiply
};
CalcStatus status = CalcStatus.None;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.