-3

点击“选择媒体和预计资金”后,查看“所需机构数量”的结果。然后从左侧的代理列表框中选择代理的名称,然后单击“添加代理”。单击“添加机构:”按钮时,所选机构的名称应出现在机构列表旁边的列表框中。这是代理列表,应该在左侧的代理列表框中输入。

U-Ad ($350), Striker ($190), NewAd ($250), Samson ($530), J & R ($420), Victory ($120)。

单击“添加机构:”按钮时,计算的估计成本结果应显示在“总估计成本”旁边的文本框中。例如,如果您选择“U-Ad”和“Samson”,则总估计成本为 880.00 美元;当您单击“添加代理”时,结果应该会出现:当您添加更多代理时,应该更新这样计算的估计成本。

Public Class Form1

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

    Dim interestRate, balance, initialBalance 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.05 * 0.07

    End If

    initialBalance = InputBox("Please Enter a balance between $500.00 and $3000.00")

    If initialBalance > 3000 Then

        InputBox("Please enter a number equal to or below $3000.00 and no less than $500.00")

    ElseIf initialBalance < 500 Then

        InputBox("Please enter a number equal to or above $500.00 and no more than $3000.00")

    Else

        balance = initialBalance * (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)
    dict.Add("Striker", 190)
    dict.Add("New Ad", 250)
    dict.Add("Samson", 530)
    dict.Add("J & R", 420)
    dict.Add("Victory", 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)

        total += dict(selectedItem)

    Next

    txtBoxEstimatedCost.Text = 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

结束类

修改后的代码!

4

2 回答 2

2

我更喜欢List(Of T)到数组。我使用了 Integer,因为它似乎不需要 Double 类型。

Dim myList As New List(Of Integer)
myList.Add(350)' etc...
'add the items
Dim total as Integer = myList.Sum()
于 2013-03-23T03:34:11.080 回答
1

这是 Windows 窗体吗?我将假设它是的,尽管它可以通过一些工作转移到 WebForms 上下文中。

我假设您的 ListBox 只是充满了字符串。如果是这样,那么您可以设置如下:

    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)

The Dictionary is a great collection to know, BTW.

Then, instead of your current code you could do something like:

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 = total.ToString();

Note the difference from your code where we cherry-pick the things to add out of the dictionary rather than try to add the entire list (which is what While (p < list.Length()) would do). Also, we just assign the Text of txtBoxEstimatedCost once whereas in your loop it updates at each iteration, which is probably not needed since you only should display the final value.

Note that this isn't the best way to do this and you may be better served following an example like here using the DisplayMember and ValueMember properties.

This makes adding to your list a little harder because you have to add a more complex type (i.e. a class you create) rather than just a String but in the long run I think it is a better approach.

于 2013-03-23T03:41:32.443 回答