2013-03-23 70 views
-3

點擊後檢查的「機構必須數」結果「選擇媒體和預估基金。」然後從左邊的代理列表框中選擇該機構的名稱並單擊「添加代理商」。單擊「添加代理商:」按鈕後,所選代理商的名稱應顯示在代理列表旁邊的列表框中的 。以下是機構列表,應在左邊的機構列表框中輸入。需要幫助,我在Visual Basic數組,不添加正確

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 

末級

修改過的代碼!

+1

當您有解決方案時,請不要刪除您的問題的文本。你的問題文本將有助於未來的訪問者有同樣的問題。這是Stack Overflow的目的。謝謝! – 2013-03-24 01:17:42

回答

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) 

Dictionary是一個偉大的收集知道,BTW。

然後,而非目前的代碼,你可以這樣做:

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(); 

注意你的代碼中的區別在哪裏,我們的櫻桃挑東西添加了字典,而不是嘗試添加整個列表(這是While (p < list.Length())會做的)。此外,我們只分配 txtBoxEstimatedCost一次,而在您的循環中它會在每次迭代時更新,這可能不是必需的,因爲您只應顯示最終值。

注意這不是最好的方式來做到這一點,你可能會更好地爲here使用DisplayMember和ValueMember屬性的例子。

這使得加入列表有點困難,因爲你必須添加更復雜的類型(即創建一個類),而不是隻是一個字符串,但是從長遠來看,我認爲這是一個更好的辦法。

+0

它幾乎可以工作!它只是不喜歡「總+ =字典[selectedItem] .Value」行...... – user2201396 2013-03-23 03:50:07

+0

你會得到什麼錯誤? – nkvu 2013-03-23 04:00:24

+0

其實,沒關係這是我的錯誤,它應該只是'總+ + =字典[selectedItem]' – nkvu 2013-03-23 04:00:59

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()