2016-05-11 38 views
1

我正在构建一个C#程序,允许用户单击一组数字按钮,然后在标签控件上显示结果。对于下面的代码,如果用户点击$ 10,$ 20和$ 50按钮,结果应该显示为$ 80。如何在C#Windows窗体应用程序中分别显示数字结果?

enter image description here

但是,如果用户想从“0”到“9”,例如,用户想输入“35”进入标签控制输入数字按钮,用户需要输入“3”和“5”。不幸的是,结果显示8,而不是显示为35.

enter image description here

所以,我怎样才能改善这种代码?

从“0”到“9”数字按钮,click事件是button_click,并且$ 10,$ 20,$ 50按钮,click事件是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('$'))); 
     } 
+0

从这段代码中,当用户分别输入“$ 20”和8个按钮时,结果可以计算出“$ 28”。 – Clement

+0

你的意思是手动输入例如3 + 5的值?它会返回相同标签上的值? – Usman

+0

是的,我希望当用户分别输入3个和5个按钮时,标签将显示35。 – Clement

回答

3

只要乘以10将第二(或第三,第四等)前值?

DollarTotalCount = (DollarTotalCount * 10) + (Convert.ToDecimal(button.Text)); 
+1

它看起来很有用,谢谢。 – Clement

+0

不错的工作 – Usman

相关问题