2013-09-25 3 views
1

나는 수업을위한이 과제에 전적으로 매달렸다. 나는 판매 세와 퍼센트를 계산하는 프로그램을 가지고있다. 그러나 나는 3 개의 누적 된 텍스트 박스를 가질 필요가있다. 즉 사용자가 부분합을 입력하면 변수에 저장하고 다음에 입력 할 때 동일한 변수에 추가하여 표시합니다. 나는 이것에 시간에 있었고 운이 없으며 오류가 계속 발생합니다.사용자가 입력 한 값을 누적하는 방법은 무엇입니까?

Dim numberOfInvoices As Integer 
Dim totalOfInvoices As Decimal 
Dim invoiceAverage As Decimal 

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click 
    Dim subtotal As Decimal = CDec(txtEnterSubtotal.Text) 
    Dim discountPercent As Decimal = 0.25D 
    Dim discountAmount As Decimal = Math.Round(subtotal * discountPercent, 2) 
    Dim invoiceTotal As Decimal = subtotal - discountAmount 
    Dim accumSubtotal As Decimal = subtotal 

    txtSubtotal.Text = FormatCurrency(subtotal) 
    txtDiscountPercent.Text = FormatPercent(discountPercent, 1) 
    txtDiscountAmount.Text = FormatCurrency(discountAmount) 
    txtTotal.Text = FormatCurrency(invoiceTotal) 

    numberOfInvoices += 1 
    totalOfInvoices += invoiceTotal 
    invoiceAverage = totalOfInvoices/numberOfInvoices 

    Me.txtNumberOfInvoices.Text = numberOfInvoices.ToString 
    Me.txtTotalOfInvoices.Text = FormatCurrency(totalOfInvoices) 
    Me.txtInvoiceAverage.Text = FormatCurrency(invoiceAverage) 


    '-------This is where i've been trying to accumulate values------' 
    'I need to accumulate the subtotal everytime the user enters something 
    'txtAccumSubtotal.text = 'the variable that adds evertime a new number is input into txtEnterSubtotal.text 

    txtEnterSubtotal.Text = "" 
    txtEnterSubtotal.Select() 
    'This is a comment 
End Sub 

코드에서이 권리를 설명하기를 바랍니다. 이 일에 정말로 도움이 필요해.

답변

1

클릭 할 때마다 현재 소계가 AccumSubTotal에 할당됩니다. Accum Sub를 Click 이벤트 외부에 선언 한 다음 새 SubTotal을 추가합니다.

시도 해보세요. 코드를 보여 주겠지 만, 배우고 싶어? 힌트 :

AccumSubTotal += subtotal 

또는 오래된 학교

AccumSubTotal = AccumSubTotal + subtotal 
+0

감사합니다! 알았다. 그 힌트가 필요하지 않았다 :) –

관련 문제