我正在制作一个由三个事件程序组成的程序(换句话说,三个不同的任务按钮:“选择媒体和估计基金”,“添加代理商”和“生成报告”)。
当点击“选择媒体和估计基金”按钮时,弹出输入框并要求用户输入余额,该余额将通过公式(余额=余额*(1 +利率))。用户输入的余额不能低于$ 500.00或超过$ 3000.00。
(估算预算应根据您选择的资金来计算(资金资源是他们选择的帐户类型,储蓄(7%利率)和企业(5%利率)计算结果应出现在残疾人“估计预算”文本框旁边的文本框。如果您选择了两个资源,则应将每个估算预算汇总在一起。计算过程应称为事件过程中定义为“FundingResource(Balance)”的子过程。
请注意当前年度,2013年。现在,报告是在2013年准备的,但应计算所选年份的余额(例如,2015年,2016年,2017年)。考虑重复的主题,以确定所选年份的期末余额。
我已经完成了我认为的代码,但不知道如何将其放入Sub Procedure。上述问题的代码在“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 :(得分:0)
您的输入框不正确,如果一直输入无效值,则例程将进入下一行代码!
它们应如下所示。
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”程序中,因为没有计算总值,也没有考虑到计算是针对未来年份这一事实。事实上,KEY CALCULATION部分缺失了!
我的意思是,如果您希望生成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
我相信您已经知道参数和返回数据类型可以是任何可接受或可识别的数据类型。
关于你的上一条评论(从...开始。另外,我有一个小..)如果你交换代码并将你的ORIGINAL代码放回去,你仍然会有同样的问题!
好的,最后一个代码示例和指针。
所以要停止你的程序(10%你的90%我的hahaha)要求输入你可以把它包含在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年的利息,这就是你问题的这一部分所要求的......
请注意当前的2013年。现在,报告是在2013年准备的,但应计算所选年份的余额(例如,2015年,2016年,2017年)。考虑重复的主题,以确定所选年份的期末余额