如何修复索引超出范围

时间:2013-10-12 18:43:56

标签: .net vb.net winforms

我有以下代码。当我故意输入错误的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

2 个答案:

答案 0 :(得分: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

在某些应用中,列出列表中的每个可能选项都是不可行的,因此仅列出了最可能的选项。用户可以输入完全不同的选项作为完全有效的选项。在添加新项类型应用程序中,SelectedIndex为-1是要执行此操作的信号。

正如您最后发现的那样,您可以使用限制列表方式使用组合框,这意味着用户无法输入不在列表中的值。这不是一个修复,而是一种不同的操作模式或风格。此操作模式不适用于上述其他两个用例。

答案 1 :(得分:0)

我个人的建议是在分析之前始终比较长度。

你有2个组合框,每个组合框都有可变的大小。

尝试以下想法:

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

或者,添加一些 throw ... catch 语句。