禁止文本框中的字母和特殊字符

时间:2017-12-14 06:08:46

标签: vb.net

我有一个程序,用户在字段中输入数量并自动计算价格。我想只允许这个字段中的数字,并且无法找到一种方法。

我当前拥有的是一个小的弹出消息,当用户输入超过1000的数字时会发生这种消息。但是如果他们只是单击“确定”,它仍然允许他们使用它。

  Private Sub txt2x6LumberQuantity_TextChanged(sender As Object, e As EventArgs) Handles txt2x6LumberQuantity.TextChanged
        'Text changed event that occurs when the text within the textbox is changed'
        'If statement to check if the number is between 0 and 1000, if is it'll go to the next block of code and if not goes to the else'
        If IsNumeric(txt2x6LumberQuantity.Text) = True Then
            Select Case CInt(txt2x6LumberQuantity.Text)
                Case 0 To 1000
                    'Calculates line total with a function'
                    dec2x6LumberLineTotal = calculate2x6LumberLineTotal(CDec(lbl2x6LumberPrice.Text), CDec(txt2x6LumberQuantity.Text), lbl2x6LumberLineTotal)
                    'Calculates the subtotal'
                    calculateSubtotal(dec2x6LumberLineTotal, dec2x4LumberLineTotal, decOneHalfPlywoodLineTotal, decFiveEighthsPlywoodLineTotal, decNailsLineTotal, decBradsLineTotal, decGalvanizedScrewLineTotal, decSledgeHammerLineTotal, decFiveDrillBitsLineTotal, decStapleGunLineTotal)
                    'Calculates the tax'
                    calculateTax(decSubtotal)
                Case Else
                    MsgBox("Choose a quantity between 0 and 1000", MsgBoxStyle.Information, "Quantity entered is invlaid.")
                    txt2x6LumberQuantity.Focus()
            End Select
        End If
    End Sub

1 个答案:

答案 0 :(得分:0)

实施例: 使用有效的事件程序

Private Sub txtAge_Validate(Cancel As Boolean)
If Not IsNumeric(txtAge.Text) Then
Cancel = True
ElseIf txtAge.Text < 21 Then
Beep 'give the user some minimal feedback
MsgBox "Enter an age greater than 21"
Cancel = True
'Following is not needed. Placed here for clarity
Else
Cancel = False
End If
End Sub