我有以下代碼。並且我一直在編寫一個錯誤「索引超出範圍,當我故意輸入錯誤的combox產品編號」時,此索引超出範圍.... ProductSalesTotalDecimal(IndexInteger)+ =(txtPriceAmount.Text * txtQuantityAmount.Text)「如何修復索引超出範圍
這只是當我點擊組合框向下箭頭拉入正確的數字,但然後退格改變它是錯誤的,否則當我啓動程序並手動輸入數組到combox驗證和工作正常。修復?
Private Sub PurchaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PurchaseToolStripMenuItem.Click
'Test to determine if a product was found.
If txtDescription.Text = String.Empty Then
'Cannot purchase, product was not found
MessageBox.Show("You must select a valid product before purchasing.", "Cannot Purchase", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
txtProductID.Focus()
txtProductID.SelectAll()
Else
'Can purchase the product
'Build a string to display in the listbox control
Dim ProductString As String = txtProductID.Text.PadRight(12, " ") & "" & txtDescription.Text.PadRight(50, " ") & "" & txtQuantityAmount.Text.PadRight(7, " ") & "" & txtPriceAmount.Text.PadLeft(9, " ").ToString
lstPurchaseItems.Items.Add(ProductString).ToString()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Accumulate the total value of this customer order
'and display it to the output textbox
TotalDueDecimal += (txtPriceAmount.Text.ToString * txtQuantityAmount.Text)
txtTotalDueAmount.Text = TotalDueDecimal.ToString("C2")
'TotalDueTextBox.Text = QuantityTextBox.Text * TotalDueDecimal.ToString("C2")
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Accumulate total sales by product to an array
Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex
ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text)
'Here you can clear the form of product info if you think
'that is a good way to do the processing
cboProductIDLookup.SelectedIndex = -1
txtProductID.Clear()
txtDescription.Clear()
txtPriceAmount.Clear()
txtQuantityAmount.Clear()
txtProductID.Focus()
End If
End Sub
什麼行引發異常? – yossico
是'ComboBox'中'cboProductIDLookup.SelectedIndex'選擇的項目嗎?否則,您會在'ProductSalesTotalDecimal(IndexInteger)'的下一行獲得該異常。 –
如果將文本部分的內容更改爲「錯誤」,則cboProductIDLookup.SelectedIndex將爲-1,在捕獲值之前測試該值 – Plutonix