2013-10-12 66 views
-1

我认为我有finnaly找到了我对不同问题的回答,但该程序像下面的代码一样使用我的任何格式。我需要格式化该行:如何设置货币格式

Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString

具体来说,我需要txtPriceAmount.Text.PadLeft(9, " ").ToString接受货币格式( “C2”)。我究竟做错了什么??

Private Sub PurchaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseToolStripMenuItem.Click 

    'Test to determine if a product was found. 
    If txtDescription.Text = String.Empty Then 

     'Cannot purchase, product was not found 
     MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     txtProductID.Focus() 
     txtProductID.SelectAll() 
    Else 
     'Can purchase the product 

     Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString 
     lstPurchaseItems.Items.Add(ProductString) 
     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     'Accumulate the total value of this customer order 
     'and display it to the output textbox 
     TotalDueDecimal += (txtPriceAmount.Text.ToString * txtQuantityAmount.Text) 
     txtTotalDueAmount.Text = TotalDueDecimal.ToString("C2") 
     'TotalDueTextBox.Text = QuantityTextBox.Text * TotalDueDecimal.ToString("C2") 

     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 


     'Accumulate total sales by product to an array 
     Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex 
     ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text) 

     'Here you can clear the form of product info if you think 
     'that is a good way to do the processing 
     cboProductIDLookup.SelectedIndex = -1 
     txtProductID.Clear() 
     txtDescription.Clear() 
     txtPriceAmount.Clear() 
     txtQuantityAmount.Clear() 
     txtProductID.Focus() 
    End If 
End Sub 
+0

如果我改为(“C2”)我得到这个错误。从字符串“C2”转换为“整数”类型无效。 –

+0

如何发布清晰易读的代码?我甚至无法重新格式化您发布的内容,因为我不知道所有“....”意味着什么 –

+0

一种澄清问题的方式能够读取它。蓝色框中的代码编译 –

回答

2

您需要txtPriceAmount.Text转换为数字,然后格式化为货币:

Double.Parse(txtPriceAmount.Text).ToString("C2").PadLeft(9, " ") 

如果txtPriceAmount.Text不是数字你会得到一个错误。

+0

谢谢先生。我一直在为这个简单的解决方案工作几天。文本框只接受带有自动小数位置的数字输入和从右到左的权限。谢谢一堆 –

+0

我有一个问题,因为价格不能为00.00。引发异常。我该如何解决这个问题 –

+0

“00.00”适合我。什么是例外? –