【发布时间】:2011-10-23 03:19:03
【问题描述】:
private void XButtonExit_Click(object sender, EventArgs e) { //这将关闭程序 关(); }
private void xButtonClear_Click(object sender, EventArgs e)
{
//This will clear all the Text Boxes
xTextBoxQuantity.Clear();
xTextBoxPrice.Clear();
xTextBoxRecieved.Clear();
xTextBoxSubtotal.Clear();
xTextBoxTotal.Clear();
xTextBoxReturn.Clear();
//This will turn the Return box and Lable back to hidden
xTextBoxReturn.Visible = false;
xLableReturn.Visible = false;
}
private void XButtonBalance_Click(object sender, EventArgs e)
{
//This will make the Return box and Lable visable
xLableReturn.Visible = true;
xTextBoxReturn.Visible = true;
//Take value from xTextBoxTotal and store it
Double Total = Convert.ToDouble(xTextBoxTotal.Text);
//Take value from xTextBoxRecieved and store it
double Recieved = Convert.ToDouble(xTextBoxRecieved.Text);
//Take value from xTextBoxTotal and subtract from amout recieved
double Amount = Total - Recieved;
//Take the Amount and store it in xTextBoxReturn
xTextBoxReturn.Text = Convert.ToString(Amount);
//Change color, Red for amount owed and green for amout to give back
if (Amount < .01) xTextBoxReturn.BackColor = Color.Green;
else xTextBoxReturn.BackColor = Color.Red;
}
private void XButtonTotal_Click(object sender, EventArgs e)
{
//Take value from xTextBoxQuantity and store it
Double num1 = Convert.ToDouble(xTextBoxQuantity.Text);
//Take value from xTextBoxPrice and store it
Double num2 = Convert.ToDouble(xTextBoxPrice.Text);
//Peform Muptlication and store it in xTextBoxSubtotal
Double Subtotal = num1 * num2;
xTextBoxSubtotal.Text = Convert.ToString(Subtotal);
//Take the Subtotal and add a 6% sales tax and store it in xTextBoxTotal
Double SalesTax = Subtotal * 1.06;
xTextBoxTotal.Text = Convert.ToString(SalesTax);
这是我当前的代码,它确实很好用。问题是,我需要将所有 TextBoxes 转换为货币格式。当我尝试数学不再有效。任何想法都会有所帮助。我遇到的最大问题是将其转换为货币时的销售税。如果小计采用货币格式,我无法进行数学运算。我试图将其转换回十进制,但是当我这样做时,我无法运行命令 Subtotal * 1.0
这是我改变的:
//从xTextBoxTotal中取值并存储
Convert.ToInt16(xTextBoxTotal.Text);
Double Total = Convert.ToDouble(xTextBoxTotal.Text);
//Take value from xTextBoxRecieved and store it
double Recieved = Convert.ToDouble(xTextBoxRecieved.Text);
//Take value from xTextBoxTotal and subtract from amout recieved
double Amount = Total - Recieved;
//Take the Amount and store it in xTextBoxReturn
xTextBoxReturn.Text = Amount.ToString("C");
//Change color, Red for amount owed and green for amout to give back
if (Amount < .01) xTextBoxReturn.BackColor = Color.Green;
else xTextBoxReturn.BackColor = Color.Red;
}
private void XButtonTotal_Click(object sender, EventArgs e)
{
//Take value from xTextBoxQuantity and store it
Double num1 = Convert.ToDouble(xTextBoxQuantity.Text);
//Take value from xTextBoxPrice and store it
Double num2 = Convert.ToDouble(xTextBoxPrice.Text);
//Peform Muptlication and store it in xTextBoxSubtotal
Double Subtotal = num1 * num2;
xTextBoxSubtotal.Text = Subtotal.ToString("C");
//Take the Subtotal and add a 6% sales tax and store it in xTextBoxTotal
Double SalesTax = Subtotal * 1.06;
xTextBoxTotal.Text = SalesTax.ToString("C");
我的错误是在 Convert.toInt16(xtextboxTotal.text) 上未处理 FormatException
【问题讨论】:
-
如果我更改 xTextBoxSubtotal.Text = subtotal.toString("C") 我无法让销售税部分发挥作用
-
你能解释一下'让数学工作'吗?您如何在文本框中制作货币格式,您是否使用 ToString("") 方法?请编辑问题以显示不起作用的源代码。