2013-10-12 37 views
0

我有以下代碼。並且我一直在編寫一個錯誤「索引超出範圍,當我故意輸入錯誤的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 
+0

什麼行引發異常? – yossico

+0

是'ComboBox'中'cboProductIDLookup.SelectedIndex'選擇的項目嗎?否則,您會在'ProductSalesTotalDecimal(IndexInteger)'的下一行獲得該異常。 –

+0

如果將文本部分的內容更改爲「錯誤」,則cboProductIDLookup.SelectedIndex將爲-1,在捕獲值之前測試該值 – Plutonix

回答

0

我個人的建議是分析之前總是比較長。

您有兩個組合框,每個組合框都有可變的大小。

嘗試以下想法:

if ((Combobox1.SelectedIndex <= (Combobox2.Items.Count - 1)) and 
(Combobox2.SelectedIndex <= (Combobox1.Items.Count - 1))) then 
    //operation 
else 
    //error 
end if 

Alternitively,把一些 ... 語句。

1
'Accumulate total sales by product to an array 
    Dim IndexInteger As Integer = cboProductIDLookup.SelectedIndex 

沒有'程序錯誤'只是一個你沒有考慮到的情況。如果控制列表DropDowntype設置爲DropDown,則可以從列表中選擇某些內容或鍵入內容。在某些應用程序中,鍵入新內容可將該項目添加到數據源中。

在這種情況下,或者當用戶輸入錯誤的值時,combo.SelectedIndex將爲-1。這是由設計,很容易測試:

' you missed this 
    If cboProductIDLookup.SelectedIndex = -1 Then 
     ' Post error/warning message 
     ' or 
     ' add new item 
     ' as appropritate 
    End If 

在一些應用簡單說就是不列出列表中的每個可能的選擇是可行的,因此只有排名可能選項中列出。用戶可以輸入完全不同的內容作爲完全有效的選項。在添加新項目類型的應用程序中,-1的SelectedIndex是要執行此操作的信號。

正如你遲來的發現,你可以讓組合框工作在限制到列表方式意味着用戶不能輸入一個值不在列表中。這不是一個修復,而是一種不同的操作模式或風格。這種操作模式不適用於上述其他兩種用例。