0

为什么我的价格框记得我之前输入的内容。我不希望这种情况发生。有什么建议么?这是我的代码。有谁知道如何防止这种情况发生。我第二次在 pricebox 中输入价格时,它会记住之前的金额。

  Private Sub ProductIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboProductIDLookup.SelectedIndexChanged


    'Test to determine if a product has been selected
    If cboProductIDLookup.SelectedIndex <> -1 Then

        'Store the selectedIndex to variable
        Dim RowInteger As Integer = cboProductIDLookup.SelectedIndex

        'Based on RowInteger, display values to TextBox controls
        'from the array named inventoryProduct
        txtProductID.Text = InventoryProduct(RowInteger).ProductIDString
        txtDescription.Text = InventoryProduct(RowInteger).DescriptionString
        txtQuantityAmount.Text = InventoryProduct(RowInteger).QuantityInteger.ToString("N0")
        txtPriceAmount.Text = InventoryProduct(RowInteger).PriceDecimal.ToString("C2")

    End If
    ' txtQuantityAmount.Focus()
    txtPriceAmount.Focus()
    txtPriceAmount.Clear()



End Sub


    Private Sub txtPriceAmount_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPriceAmount.KeyPress


    '''''''' Check for the flag being set in the KeyDown event.
    If acceptableKey = False Then
        '''''''' Stop the character from being entered into the control since it is non-numerical.
        e.Handled = True
        Return
    Else
        '''''''' 'must be in first position            

        If e.KeyChar = Convert.ToChar(Keys.Back) Then

            If strCurrency.Length > 0 Then
                strCurrency = strCurrency.Substring(0, strCurrency.Length - 1)
            End If
        Else
            strCurrency = strCurrency & e.KeyChar
        End If

        If strCurrency.Length = 0 Then
            txtPriceAmount.Text = ""
        ElseIf strCurrency.Length = 1 Then
            txtPriceAmount.Text = "0.0" & strCurrency
        ElseIf strCurrency.Length = 2 Then
            txtPriceAmount.Text = "0." & strCurrency
        ElseIf strCurrency.Length > 2 Then
            txtPriceAmount.Text = strCurrency.Substring(0, strCurrency.Length - 2) & "." & strCurrency.Substring(strCurrency.Length - 2)
        End If
        txtPriceAmount.Select(txtPriceAmount.Text.Length, 0)

    End If
    e.Handled = True

End Sub

      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

        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)

        ' Double.Parse(txtPriceAmount.Text).ToString("C2").PadLeft(9, " ")
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Accumulate the total value of this customer order
        'and display it to the output box
        TotalDueDecimal += (txtPriceAmount.Text.ToString * txtQuantityAmount.Text)
        txtTotalDueAmount.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

这通常会在 ASP.NET/web 应用程序中发生(并且可以关闭),但是您的应用程序是一个 winforms 应用程序,因此除非您在系统中安装了一些 3rd 方组件为您执行此操作,否则它不应该发生。您需要关闭该组件。

现在,您没有指定它发生的位置。如果它是一个下拉列表,那么是的,它将自动完成(Windows/IE 设置的一部分)。如果它是一个文本框,那么没有太多可解释的。

要为下拉列表关闭它,请在设计器中将 AutoCompleteMode 设置为“none”。如果绑定了自定义源,则同样适用于文本框。

这看起来也很有趣:

 txtPriceAmount.Text = InventoryProduct(RowInteger).PriceDecimal.ToString("C2")

这也可能是问题所在,也许您的 RowInteger 没有在您的代码中的某处正确更新,因此您认为它记住了先前的输入,但实际上它只是错误的索引。

于 2013-10-12T23:16:13.373 回答
0

按键事件中的代码很可能是罪魁祸首。记下程序,看看它是否消失。

由于strCurrency未在该过程中声明它必须具有更大的值scope,因此它从上次“记住”(特别是因为您没有在过程中对其进行初始化)。

规则 #32 == LESS 代码更好。

规则 32a== 几乎在您跟踪用户击键的任何时候,您要么解决了错误的问题,要么以错误的方式解决了它。

编辑:

正如安德鲁所说,这也是错误的:

ProductSalesTotalDecimal(IndexInteger) += _
       (txtPriceAmount.Text * txtQuantityAmount.Text)

就像您不能将“猫”乘以“自行车”一样,即使文本框中有数字,您也不能将文本框的(字符串)内容相乘。

于 2013-10-12T23:21:16.797 回答