2013-03-23 38 views
-1

我正在研究一個由三個事件過程組成的程序(換句話說,三個不同的任務按鈕:「Select Media and Estimated Fund」,「添加代理商「和」生成報告「)。幫助創建名爲「(FundingResource(Balance))」的子過程Visual Basic

當點擊「Select Media and Estimated Fund」按鈕時,彈出輸入框並要求用戶輸入餘額,該餘額將通過公式(Balance = Balance *(1 + interest rate) 。用戶輸入的餘額不能低於500.00美元或高於3000美元。

(預算預算應該根據您選擇的資金來計算(資金來源是他們選擇的賬戶類型,儲蓄(7%利息)費率)和公司(5%利率)計算結果應顯示在「預算預算」文本框旁邊的禁用文本框中。如果選擇了兩個資源,則應將每個預算預算彙總在一起,計算過程應稱爲子程序def在事件過程中被定義爲「FundingResource(餘額)」。

請注意本年度,2013年。現在,報告在2013年編制,但餘額應計算在您選定的一年(例如2015年,2016年,2017年)。考慮重複的話題以確定選定年份的期末餘額。

我已經完成了我相信的代碼,但不知道如何將它放入Sub過程。上述問題的代碼在「btnMediaEstimatedFund」中。

Public Class Form1 

Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click 

    Dim interestRate, balance, initialBalanceSavings, initialBalanceCorporate, finalBalance As Double 

    txtBoxEstimatedBudget.Enabled = False 
    txtBoxAgenciesNeeded.Enabled = False 

    If radButtonTraditional.Checked Then 

     txtBoxAgenciesNeeded.Text = 3 

    ElseIf radButtonEMedia.Checked Then 

     txtBoxAgenciesNeeded.Text = 2 

    End If 

    If checkBoxSavings.Checked Then 

     interestRate = 0.07 

    ElseIf checkBoxCorporate.Checked Then 

     interestRate = 0.05 

    ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then 

     interestRate = 0.12 

    End If 

    initialBalanceSavings = InputBox("Please Enter a balance for SAVINGS account between $500.00 and $3000.00") 

    If initialBalanceSavings > 3000 Then 

     InputBox("Please enter a balance for SAVINGS account equal to or below $3000.00 and no less than $500.00") 

    ElseIf initialBalanceSavings < 500 Then 

     InputBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00") 

    End If 

    initialBalanceCorporate = InputBox("Please Enter a balance for CORPORATE account between $500.00 and $3000.00") 

    If initialBalanceCorporate > 3000 Then 

     InputBox("Please enter a balance for CORPORATE account equal to or below $3000.000 and no less than $500.00") 

    ElseIf initialBalanceCorporate < 500 Then 

     InputBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00") 

    Else 

     finalBalance = initialBalanceCorporate + initialBalanceSavings 
     balance = finalBalance * (1 + interestRate) 
     txtBoxEstimatedBudget.Text = balance 

    End If 

End Sub 


Private Sub btnAddAgencies_Click(sender As Object, e As EventArgs) Handles btnAddAgencies.Click 

    Dim dict As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)() 
    dict.Add("U-Ad ($350)", 350) 
    dict.Add("Striker ($190)", 190) 
    dict.Add("New Ad ($250)", 250) 
    dict.Add("Samson ($530)", 530) 
    dict.Add("J & R ($420)", 420) 
    dict.Add("Victory ($120)", 120) 

    Dim selectedItems = (From i In lstBoxAgenciesList.SelectedItems).ToArray() 
    Dim total As Integer = 0 

    For Each selectedItem In selectedItems 

     lstBoxSelectedList.Items.Add(selectedItem) 
     lstBoxAgenciesList.Items.Remove(selectedItem) 

    Next 

    For Each item In lstBoxSelectedList.Items 

     total += dict(item) 

    Next 

    txtBoxEstimatedCost.Text = FormatCurrency(total.ToString()) 

End Sub 

Private Sub btnGenerateReport_Click(sender As Object, e As EventArgs) Handles btnGenerateReport.Click 

    Dim employeeLevel, year, selectedMedia, numberOfAgencies As String 
    Dim today As Date 

    today = CStr(dtpToday.Text) 
    Name = CStr(txtBoxCreator.Text) 
    employeeLevel = CStr(lstBoxResults.Text) 
    year = CStr(lstBoxResults.Text) 
    selectedMedia = CStr(lstBoxResults.Text) 
    numberOfAgencies = CStr(txtBoxAgenciesNeeded.Text) 

    dtpToday.Text = FormatDateTime(today, DateFormat.ShortDate) 

    If radButtonManager.Checked Then 

     employeeLevel = "Manager" 

    ElseIf radButtonStaff.Checked Then 

     employeeLevel = "Staff" 

    End If 

    If radButton2015.Checked Then 

     year = "2015" 

    ElseIf radButton2016.Checked Then 

     year = "2016" 

    ElseIf radButton2017.Checked Then 

     year = "2017" 

    End If 

    If radButtonTraditional.Checked Then 

     selectedMedia = "Traditional Media (TV, Radio)" 

    ElseIf radButtonEMedia.Checked Then 

     selectedMedia = "New e-Media (SNS, e-Mail)" 

    End If 

    lstBoxResults.Items.Add("=======================================") 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Date : " & today) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Reporting Entity : " & Name) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Level of Employee : " & employeeLevel) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("=======================================") 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Year" & " " & year & " " & "Budget of Advertising Report") 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("=======================================") 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Total Estimated Cost : " & FormatCurrency(txtBoxEstimatedCost.Text)) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Total Estimated Budget : " & FormatCurrency(txtBoxEstimatedBudget.Text)) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Selected Media : " & selectedMedia) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("Number of Agencies Involved : " & numberOfAgencies) 
    lstBoxResults.Items.Add(" ") 
    lstBoxResults.Items.Add("=======================================") 

End Sub 

End Class 
+0

好吧,那麼你堅持在哪一點? – Zeddy 2013-03-24 02:16:00

+0

我有一個問題,編寫子程序來獲得平衡! – user2201396 2013-03-24 03:33:15

+0

如果問題被正確回答,請將正確答案標記爲「已接受」。不要只是刪除問題文本。 – 2013-03-25 20:48:14

回答

0

更新

你inputboxes是不正確的,如果輸入的值無效一貫的程序將通過拖放到代碼的下一行!

它們應該如下所示。

Do 
    inputtedData = InputBox("Please Enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00") 
    If inputtedData = "" Then 
    MsgBox("User chose to Cancel calculation!") 
    Exit Sub 
    Else 
    initialBalanceSavings = CType(inputtedData, Single) 
    If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error") 
    End If 
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000 

Do 
    inputtedData = InputBox("Please Enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00") 
    If inputtedData = "" Then 
    MsgBox("User chose to Cancel calculation!") 
    Exit Sub 
    Else 
    initialBalanceCorporate = CType(inputtedData, Single) 
    If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error") 
    End If 
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000 

最後,所有的代碼是絕對不是在「btnMediaEstimatedFund」程序,因爲有總價值沒有計算,也不是給定的事實,計算是未來幾年的任何考慮。實際上「關鍵計算」部分已丟失!

我的意思是如果你想爲2016年產生一個計算數字,現在是2013年,那麼你的會計利息在7%和5%在哪裏?我看不到任何實際計算的地方。

您「不知道」如何創建的子例程與您爲按鈕/文本框/列表框事件創建的子例程沒有區別!

你傳入參數,並進行計算,如果它是一個SUB它就結束了!如果它是一個函數,那麼它返回一個值或一個對象。

要創建一個Sub例程,您需要爲它指定一個參數列表,如果需要的話,您可能會有不帶參數的子例程和函數,但函數必須總是返回一個值/對象。

Sub ThisIsMySubroutine(MyParam1 as string, My Param2 as Object) 
    ' code here 
End Sub 


Function ThisIsMyFunction(MyParam1 as string, My Param2 as Object) As Boolean 
    ' code here 
End Function 

我確定您已經知道參數和返回數據類型可以是任何可接受或可識別的數據類型。

更新II

至於你的最後一個註釋(開頭..此外,我有一個小..)如果你交換代碼,並把你的ORIGINAL代碼回來,你仍然會有同樣的問題!

好的,最後一個代碼示例和指針。

所以從要求輸入中斷程序(10%你90%的礦山哈哈哈),您可以將其括在if語句...

If checkBoxSavings.Checked Then 
    Do 
    inputtedData = InputBox("Please Enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00") 
    If inputtedData = "" Then 
     MsgBox("User chose to Cancel calculation!") 
     Exit Sub 
    Else 
     initialBalanceSavings = CType(inputtedData, Single) 
     If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error") 
    End If 
    Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000 
End If 

If checkBoxCorporate.Checked Then 
    Do 
    inputtedData = InputBox("Please Enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00") 
    If inputtedData = "" Then 
     MsgBox("User chose to Cancel calculation!") 
     Exit Sub 
    Else 
     initialBalanceCorporate = CType(inputtedData, Single) 
     If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error") 
    End If 
    Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000 
End If 

而對於這裏的興趣是一個鏈接你, http://math.about.com/od/businessmath/ss/Interest.htm

它的一個非常好的解釋,因爲它顯示了在1年以上的利息,這是你的問題,這部分是問...

請AWA現在,報告已在2013年編制完成,但餘額應根據您選定的一年(例如2015年,2016年和2017年)進行計算。考慮重複的話題來確定你所選年份的期末餘額

+0

如果餘額=平衡*(1 + interestRate)從我上面的代碼,我怎麼會計算總餘額,因爲我錯了? – user2201396 2013-03-24 05:48:45

+0

我想通過從他們選擇的複選框(儲蓄,公司或兩者)以及他們輸入到msgBox的餘額中計算餘額!請幫忙! – user2201396 2013-03-24 05:58:29

+0

另外,我在您提供的代碼中遇到了一個小問題。它要求我輸入「Saving」和「Corporate」餘額,即使用戶只檢查「Saving」複選框。你有點明白我在說什麼嗎?只應要求用戶輸入他在複選框中選中的帳戶類型!非常感謝您的幫助! – user2201396 2013-03-24 06:11:10