Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've been trying to keep the user from using letters instead of numbers in my program.

VB
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


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.

VB
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
Posted
Updated 29-Oct-17 9:51am
v2
Comments
A_Griffin 28-Oct-17 18:37pm    
If your program is crashing you need to either step through it line by line in debug mode, or at least wrap your code in Try-Catch blocks, to find out where the error is.
Member 13491139 28-Oct-17 21:11pm    
ok thx, ill try that
Ralf Meier 29-Oct-17 6:19am    
I suppose that you are speaking about an input made in a TextBox. So why do you customize a TextBox to prevent the input of letters of unwanted characters ?
Do you want to have an example for this ?
Member 13491139 29-Oct-17 15:39pm    
yes, Please I would appreciate it.
Ralf Meier 29-Oct-17 15:53pm    
see my Solution ...

1 solution

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)

VB
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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900