2011-10-22 80 views
0

private void XButtonExit_Click(object sender,EventArgs e) { //這將關閉程序 Close(); }簡單的收銀程序

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); 

這是我目前的代碼,它確實很好。問題是,我需要將所有文本框都變成貨幣格式。當我嘗試數學不再有效。任何想法都會有幫助。我將銷售稅轉換成貨幣時遇到的最大問題是。如果小計是貨幣格式,我無法完成數學工作。我試圖將其轉換回十進制,但是當我做,我不能運行命令小計* 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"); 

我的錯誤是出現FormatException了未處理的Convert.toInt16(xtextboxTotal.text)

+0

如果我改變xTextBoxSubtotal.Text = subtotal.toString(「C」),我無法獲得銷售稅部分工作 – user1008803

+0

你能否澄清「做數學作業」?你如何在文本框中製作貨幣格式,你是否使用了ToString(「」)方法?請編輯該問題以顯示不起作用的源代碼。 – simbolo

回答

0

當在格式化數字字符串作爲貨幣,你必須分析它回來之前,你可以重新將其轉換回數字形式,這取決於curency格式化規則喲你曾經在第一個地方創建號碼。

參見:http://msdn.microsoft.com/en-us/library/3s27fasw.aspx

string value; 
NumberStyles style; 
CultureInfo culture; 
double number; 

// Parse currency value using en-GB culture. 
value = "£1,097.63"; 
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol; 
culture = CultureInfo.CreateSpecificCulture("en-GB"); 
if (Double.TryParse(value, style, culture, out number)) 
    Console.WriteLine("Converted '{0}' to {1}.", value, number); 
else 
    Console.WriteLine("Unable to convert '{0}'.", value); 
// Displays: 
//  Converted '£1,097.63' to 1097.63.