2013-10-12 5 views
-1

나는 finnaly가 다른 질문에 대한 나의 답을 찾았지만, 다음 코드를 가진 어떤 형식의 프로그램처럼 생각한다. 나는이 줄을 포맷해야합니다통화 형식을 설정하는 방법

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

특히, 내가 통화 형식 ("C2")을 받아 txtPriceAmount.Text.PadLeft(9, " ").ToString이 필요합니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?? txtPriceAmount.Text가 숫자가 아닌 경우

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

당신은 오류가 발생합니다 :

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를 변환해야합니다.

+0

감사합니다. 나는 그 간단한 해결책을 며칠 동안 연구 해왔다. 텍스트 상자는 오른쪽에서 왼쪽으로 자동 소수 자릿수 및 권한 양식이있는 숫자 입력 만 허용합니다. 고마워요 –

+0

가격은 00.00이 될 수 없다는 문제가 있습니다. 처리되지 않은 예외가 발생합니다. 이 문제를 해결하려면 어떻게해야합니까? –

+0

"00.00"이 적합합니다. 예외는 무엇입니까? –

관련 문제