如何防止用户在VB中使用字母而不是数字。 [英] How do I keep a user from using letters instead of numbers in VB.

查看:81
本文介绍了如何防止用户在VB中使用字母而不是数字。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图阻止用户在我的程序中使用字母而不是数字。



I've been trying to keep the user from using letters instead of numbers in my program.

Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
        Dim hours, payPerHour, overtimeHours, pay As Decimal
        InputData(hours, payPerHour)
        CalculateValues(hours, payPerHour, overtimeHours, pay)
        DisplayData(overtimeHours, pay)
    End Sub
    Sub InputData(ByRef hours As Decimal, ByRef payPerHour As Decimal)
        If IsNumeric(txtHoursWorked.Text) = False Then
            MessageBox.Show("Please Enter A Value")
        ElseIf IsNumeric(txtHourlyPay.Text) = False Then
            MessageBox.Show("Please Enter A Value")
        End If
        If (txtHoursWorked.Text) = "" Then
            MessageBox.Show("Please Enter A Value")
        ElseIf (txtHourlyPay.Text) = "" Then
            MessageBox.Show("Please Enter A Value")
        End If
        hours = CDec(txtHoursWorked.Text)
        payPerHour = CDec(txtHourlyPay.Text)
    End Sub
    Sub CalculateValues(hours As Decimal, payPerHour As Double, ByRef overtimeHours As Decimal, ByRef pay As Decimal)
        overtimeHours = Math.Abs(40 - hours)
        If hours <= 40 Then
            pay = CDec((payPerHour * hours))
        ElseIf hours > 40 Then
            pay = CDec((CDec(payPerHour * hours) + (payPerHour * 1.5) * (overtimeHours)))
        End If
    End Sub

    Sub DisplayData(overtimeHours As Decimal, pay As Decimal)
        txtOvertime.Text = CStr(overtimeHours)
        txtWeeksPay.Text = pay.ToString("C")
    End Sub





我的尝试:



我尝试使用If语句显示一个消息框以向用户显示他们的错误,但我的程序一直在崩溃。





What I have tried:

I've tried using an If statement to show a message box to show the user their error, but my program keeps crashing.

If IsNumeric(txtHoursWorked.Text) = False Then
            MessageBox.Show("Please Enter A Value")
        ElseIf IsNumeric(txtHourlyPay.Text) = False Then
            MessageBox.Show("Please Enter A Value")
        End If
        If (txtHoursWorked.Text) = "" Then
            MessageBox.Show("Please Enter A Value")
        ElseIf (txtHourlyPay.Text) = "" Then
            MessageBox.Show("Please Enter A Value")
        End If

推荐答案

Exampel用于自定义文本框控件只允许数字输入 - 由给定的Mask定义。

(因为它原本只用于我的一些变量和描述都是德语...但我希望无论如何控制是可以理解的)



Exampel for a customized Textbox-Control which only allows Numeric inputs - defined by a given Mask.
(Because it's originally only used by me some Variables and Descriptions are in German ... but I hope the Control is understandable anyway)

Private Class NumericTextBox
    Inherits TextBox

    Property CheckLimits As Boolean
        Get
            Return myCheckLimits
        End Get
        Set(ByVal value As Boolean)
            myCheckLimits = value
        End Set
    End Property
    Private myCheckLimits As Boolean = False

    Property Minimum As Double
        Get
            Return myMinimum
        End Get
        Set(ByVal value As Double)
            myMinimum = value
        End Set
    End Property
    Private myMinimum As Double = 0

    Property Maximum As Double
        Get
            Return myMaximum
        End Get
        Set(ByVal value As Double)
            myMaximum = value
        End Set
    End Property
    Private myMaximum As Double = 100000

    Property Maske As String
        Get
            Return myMaske
        End Get
        Set(ByVal value As String)
            myMaske = value.Replace(".", ",")
            Check_TextNumeric()
            NumericMaske_zerlegen()
            formatiere_Ausgabe()
        End Set
    End Property
    Private myMaske As String = "#,000"

    Property NegativeAllowed As Boolean
        Get
            Return myNegativeAllowed
        End Get
        Set(ByVal value As Boolean)
            myNegativeAllowed = value
            formatiere_Ausgabe()
        End Set
    End Property
    Private myNegativeAllowed As Boolean = True

    Event InputComplete(ByVal e As System.Windows.Forms.KeyEventArgs)



    Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs)

        Dim Taste As Keys = e.KeyCode
        Select Case Taste
            Case Keys.Enter, Keys.Escape
                'Input Complete
                formatiere_Ausgabe()
                If myCheckLimits Then Grenzwerte_überprüfen(Text)
                'e.Handled = True
                MyBase.OnKeyDown(e)
                RaiseEvent InputComplete(e)

            Case Keys.Back
                If SelectionStart > 0 Then SelectionStart -= 1
                SelectedText = ""
                If Text = "" Then Text = "0"
                If SelectionLength = 0 Then SelectionLength = 1
                e.Handled = True
            Case Keys.Tab
                Text += ","
                SelectionStart += 1
                e.Handled = True
            Case Keys.Delete
                SelectedText = ""
                If Text = "" Then Text = "0"
                If SelectionLength = 0 Then SelectionLength = 1
                e.Handled = True
            Case Else
                Beep()
                e.Handled = True
        End Select

    End Sub

    Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)

        Dim hText As String() = Text.Replace("-", "").Split(",")
        Dim xAnzVorKomma As Integer = hText(0).Length
        Dim xAnzNachKomma As Integer = 0
        If hText.Length > 1 Then xAnzNachKomma = hText(1).Length

        Dim posKomma As Integer = Text.IndexOf(",")
        Dim isEditingVorkomma As Boolean = SelectionStart <= posKomma

        Dim Taste As Char = e.KeyChar
        Select Case Taste
            Case "0" To "9"
                If isEditingVorkomma Then
                    If (myAnzVorKomma < 0) Or (xAnzVorKomma < myAnzVorKomma) Or (SelectionLength > 1) Then SelectedText = Taste
                Else
                    If (myAnzNachKomma < 0) Or (xAnzNachKomma < myAnzNachKomma) Or (SelectionLength > 1) Then SelectedText = Taste
                End If
            Case "+"
                If myNegativeAllowed Then
                    Dim p As Integer = SelectionStart
                    If Text.Substring(0, 1) = "-" Then
                        Text = Text.Replace("-", "")
                        If p > 0 Then SelectionStart = p - 1
                    End If
                End If
            Case "-"
                If myNegativeAllowed Then
                    Dim p As Integer = SelectionStart
                    If Text.Substring(0, 1) <> "-" Then
                        Text = "-" + Text
                        SelectionStart = p + 1
                    Else
                        Text = Text.Replace("-", "")
                        If p > 0 Then SelectionStart = p - 1
                    End If
                End If
            Case ".", ","
                If (Not Text.Contains(",") Or SelectedText.Contains(",")) And (myAnzNachKomma > 0) Then
                    SelectedText = ","
                    isNachkomma = True
                End If
            Case Is < Chr(32)
                MyBase.OnKeyPress(e)
        End Select

        e.Handled = True
    End Sub

    Private isNachkomma As Boolean = False

    Protected Overrides Function IsInputKey(ByVal keyData As System.Windows.Forms.Keys) As Boolean

        If keyData = Keys.Enter Then  '???
            Return True
        ElseIf keyData = Keys.Back Then
            Return True
        Else
            Return MyBase.IsInputKey(keyData)
        End If

    End Function

    Private Sub Me_Leave(sender As Object, e As System.EventArgs) Handles Me.Leave, Me.LostFocus
        formatiere_Ausgabe()
        If myCheckLimits Then Grenzwerte_überprüfen(Text)
    End Sub

    Private Sub Grenzwerte_überprüfen(xValue As String)
        Dim myValue As Double = Val(xValue)
        If myValue < myMinimum Then
            MessageBox.Show("Der Eingabewert darf den vorgegebenen" + vbCrLf + _
                            "Minimalwert von " + myMinimum.ToString + vbCrLf + _
                            "nicht unterschreiten !" _
                            , "fehlerhafte Eingabe")
            Me.Select()
        ElseIf myValue > myMaximum Then
            MessageBox.Show("Der Eingabewert darf den vorgegebenen" + vbCrLf + _
                            "Maximalwert von " + myMaximum.ToString + vbCrLf + _
                            "nicht überschreiten !" _
                            , "fehlerhafte Eingabe")
            Me.Select()
        End If
    End Sub

    Private Sub formatiere_Ausgabe()

        If myAnzNachKomma > 0 Then
            Dim myVar As Double
            Try
                myVar = CSng(MyBase.Text)
            Catch ex As Exception
                myVar = Val(MyBase.Text)
            End Try
            Dim myFormat As String = FillString(myAnzNachKomma, "0")
            MyBase.Text = myVar.ToString("0." + myFormat)
        End If

        If myAnzVorKomma > 0 Then
            Dim myTextParts As String() = MyBase.Text.Split(",")
            Dim myTextVorkomma As Integer = myTextParts(0).Length
            Dim myZeichen As Integer = myAnzVorKomma - myTextVorkomma
            If myZeichen > 0 Then
                MyBase.Text = FillString(myZeichen, "0") + MyBase.Text
            End If
        End If

    End Sub

    Private Sub Check_TextNumeric()
        If Val(MyBase.Text) = 0 Then
            Dim sa As String() = myMaske.Split(",")
            Dim myText As String
            If sa.Length > 1 Then
                myText = "0," + FillString(sa(1).Length, "0")
                MyBase.Text = myText
            Else
                myText = "0"
                MyBase.Text = myText
            End If
        End If
    End Sub

    Private myAnzVorKomma As Integer = -1
    Private myAnzNachKomma As Integer = 3
    Private myMaxTextLength As Integer = 5

    Private Sub NumericMaske_zerlegen()

        Dim myMaskeParts As String() = myMaske.Split(",")
        If myMaskeParts.Length < 1 Then
            myAnzVorKomma = -1
        ElseIf myMaskeParts(0).Contains("#") And myMaskeParts(0).Contains("0") Then
            myAnzVorKomma = -1
        ElseIf myMaskeParts(0).Contains("#") Then
            myAnzVorKomma = -1
        ElseIf myMaskeParts(0).Contains("0") Then
            myAnzVorKomma = myMaskeParts(0).Length
        ElseIf Not myMaskeParts(0).Contains("#") And Not myMaskeParts(0).Contains("0") Then
            myAnzVorKomma = 0
            Text = "0,"
            isNachkomma = True
        End If

        If myMaskeParts.Length < 2 Then
            myAnzNachKomma = -1
        ElseIf myMaskeParts(1).Contains("#") And myMaskeParts(0).Contains("0") Then
            myAnzNachKomma = -1
        ElseIf myMaskeParts(1).Contains("#") Then
            myAnzNachKomma = -1
        ElseIf myMaskeParts(1).Contains("0") Then
            myAnzNachKomma = myMaskeParts(1).Length
        ElseIf Not myMaskeParts(1).Contains("#") And Not myMaskeParts(1).Contains("0") Then
            myAnzNachKomma = 0
        End If

        If (myAnzVorKomma > 0) And (myAnzNachKomma > 0) Then
            myMaxTextLength = myAnzVorKomma + 1 + myAnzNachKomma
        ElseIf (myAnzVorKomma = 0) And (myAnzNachKomma > 0) Then
            myMaxTextLength = 1 + 1 + myAnzNachKomma
        ElseIf (myAnzVorKomma > 0) And (myAnzNachKomma = 0) Then
            myMaxTextLength = myAnzVorKomma
        ElseIf (myAnzVorKomma < 0) Or (myAnzNachKomma < 0) Then
            myMaxTextLength = -1
        ElseIf (myAnzVorKomma = 0) And (myAnzNachKomma = 0) Then
            myMaxTextLength = -1
        End If

    End Sub

End Class


这篇关于如何防止用户在VB中使用字母而不是数字。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆