Reputation: 23
I'm building a C# program that allow user to click a set of numeric buttons and then display the result on the label control. For the code as below, if user click the $10, $20 and $50 buttons, the result should be displayed as $80.
But, if user would like to enter numeric buttons from "0" to "9", for example, user wants to enter "35" into the label control, user needs to enter "3" and "5" respectively. Unfortunately, the result displayed 8, not displayed as 35.
So, how can I improve this code?
From "0" to "9" numeric buttons, the click event are button_click, and the $10,$20,$50 buttons, the click event are subutton_click.
private decimal dollarTotal;
public decimal DollarTotalCount
{
get
{
return dollarTotal;
}
set
{
dollarTotal = value;
lblAmountPay.Text = "$" + dollarTotal.ToString() + ".0000";
}
}
private void button_click(object sender, EventArgs e)
{
if (lblAmountPay.Text == "")
{
lblAmountPay.Text = "$";
}
Button button = (Button)sender;
DollarTotalCount = DollarTotalCount + (Convert.ToDecimal(button.Text));
}
private void subutton_click(object sender, EventArgs e)
{
Button subButton = (Button)sender;
DollarTotalCount = DollarTotalCount + (Convert.ToDecimal(subButton.Text.TrimStart('$')));
}
Upvotes: 1
Views: 76
Reputation: 929
Just multiply by 10 before adding the second (or third, fourth, etc) value?
DollarTotalCount = (DollarTotalCount * 10) + (Convert.ToDecimal(button.Text));
Upvotes: 3