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 がそうするシグナルです。

遅ればせながら発見したように、コンボボックスをリストに制限する方法で機能させることができます。つまり、ユーザーはリストにない値を入力できません。これは修正ではなく、別の操作モードまたはスタイルです。この操作モードは、上記の他の 2 つの使用例には適していません。

于 2013-10-12T22:19:22.627 に答える