0

我有以下代码。并且我不断收到错误消息“当我故意输入错误的组合框产品编号时,索引超出范围。“此处的索引超出范围.... ProductSalesTotalDecimal(IndexInteger) += (txtPriceAmount.Text * txtQuantityAmount.Text)”

这仅在我单击组合框向下箭头以拉入正确的数字但然后退格将其更改为错误后才这样做。否则,当我启动程序并将数字手动输入到组合框中时,验证并正常工作。有关如何修复的任何建议?

  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
4

2 回答 2

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 是这样做的信号。

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

于 2013-10-12T22:19:22.627 回答
0

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

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

尝试以下想法:

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

或者,放置一些throw ... catch语句。

于 2013-10-12T20:55:21.077 回答