为什么我的价格框会记住我之前输入的内容。我不希望这种情况发生。有什么建议? 这是我的代码。有谁知道如何防止这种情况发生。我第二次在价格框中输入价格时会记住之前的金额。
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
答案 0 :(得分:1)
这通常发生在ASP.NET / web应用程序中(并且可以关闭)但是你的应用程序是一个winforms应用程序,所以除非你的系统中安装了一些第三方组件,否则不应该这样做。你需要关闭那个组件。
现在,您没有指定它发生的位置。如果它是一个下拉列表,那么是的,它将自动完成(部分Windows / IE设置)。如果它是一个文本框,那么就没有太多可解释的了。
要为下拉列表关闭它,请在设计器中将AutoCompleteMode设置为“none”。如果有自定义源绑定到文本框,则同样适用于文本框。
这也很有趣:
txtPriceAmount.Text = InventoryProduct(RowInteger).PriceDecimal.ToString("C2")
这也可能是问题,也许你的RowInteger没有在你的代码中正确更新,因此你认为它记得以前的输入,但实际上它只是错误的索引。
答案 1 :(得分:0)
keypress事件中的代码很可能是罪魁祸首。说出程序,看看它是否消失。
由于strCurrency
未在该过程中声明,因此它必须具有更大的scope
,因此它会从上次“记住”(特别是因为您未在过程中初始化它)。
规则#32 == LESS代码更好。
规则32a ==几乎在您跟踪用户的击键时,您要么解决错误的问题,要么以错误的方式解决问题。
编辑:
正如安德鲁所说,这也是错误的:
ProductSalesTotalDecimal(IndexInteger) += _
(txtPriceAmount.Text * txtQuantityAmount.Text)
就像你不能多次“玩”时“自行车”一样,你不能将文本框的(字符串)内容加倍,即使它们中有数字。