2013-10-12 38 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是要执行此操作的信号。

正如你迟来的发现,你可以让组合框工作在限制到列表方式意味着用户不能输入一个值不在列表中。这不是一个修复,而是一种不同的操作模式或风格。这种操作模式不适用于上述其他两种用例。