Need Help with My Array in Visual Basic, Do Not Add Correctly

advertisements

Check the results of “Number of Agencies Needed” after clicking “Select Media and Estimated Fund.” Then select the names of the agencies from the agency listbox on the left and click “Add Agencies.” When the “Add Agencies:” button is clicked, the name of the selected agencies should appear in the listbox next to the agency list. Here is the list of agencies, which should be input in the agency listbox on the left.

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

When the “Add Agencies:” button is clicked, the results of the calculated estimated cost should appear in the textbox next to “Total Estimated Cost.” For example, if you selected “U-Ad” and “Samson,” then the total estimated cost would be $880.00; the results should appear when you click “Add Agencies:” When you add more agencies, such calculated estimated costs should be updated.

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

End Class

Revised Code!


Is this a Windows Form? I'm going to work on the assumption that it is, though it would be transferable across to a WebForms context with a little work.

I am assuming that your ListBox is just full of strings. If so then you could set something up like:

    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.