Hi,

I am trying to "Show" an error message for the user in two cases:

1. If he/she did not enter a correct value for a String.
2. If he/she did not enter a correct value for a Decimal.

I know I have to use Try & Catch but each time I use them I get an error.

Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonAdd.Click

        Dim AClient As New BankAccountClass
        AClient.LastName = InputBox("Enter last name", "Enter")
        AClient.AccountNumber = InputBox("Depost Amount", "Enter")


End Sub

And this what I used for the Decimal

Try
            AClient.AccountNumber 
        Catch ex As Exception
            MessageBox.Show("Please enter a number.")
        End Try

ok for Decimal you can try

if str(val( AClient.AccountNumber)).Trim <> AClient.AccountNumber.Trim
Msgbox "Please Enter a correct decimal Number"
End If

If he/she did not enter a correct value for a String.

What type of String ?

Is that for only characters?

Like "America"

or something like

"LOCALPORT567"

What type of String ?

Is that for only characters?

Like "America"

or something like

"LOCALPORT567"

Thank you for the reply.

It's a String that the user inputs in the "InputBox".
Like in the InputBox the user should enter a name "Michael" or any character but if the user enter a number "12345" It should show an error.

Use this module

Module Validator

    Public Enum ValidationType
        Only_Digits = 1
        Only_Characters = 2
        No_Blank = 3
    End Enum

    Public Sub AssignValidation(ByRef CTRL As TextBox, ByVal Validation_Type As ValidationType)
        Dim txt As TextBox = CTRL

        Select Case Validation_Type

            Case ValidationType.Only_Digits
                AddHandler txt.Leave, AddressOf ONUM_Leave

            Case ValidationType.Only_Characters
                AddHandler txt.Leave, AddressOf OCHAR_Leave

            Case ValidationType.No_Blank
                AddHandler txt.Leave, AddressOf NOBLANK_Leave



        End Select


    End Sub

    Public Sub ONUM_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txt As TextBox = sender
        If txt.Text.Trim <> Val(txt.Text).ToString Then
            MsgBox("Enter only numeric!")
            txt.Focus()
        End If


    End Sub

    Public Sub OCHAR_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim txt As TextBox = sender
        Dim c As Integer
        For c = 0 To txt.Text.Length - 1
            If InStr("1234567890!@#$%^&*()_+=-", txt.Text.Chars(c)) > 0 Then
                Exit For
            End If
        Next
        If c <> txt.Text.Length Then
            MsgBox("Only characters are allowed!")
            txt.Focus()
        End If

    End Sub



    Public Sub NOBLANK_Leave(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim t As TextBox = sender

        If t.Text.Trim = "" Then
            MsgBox("This field Must be filled!")
            t.Focus()
        End If
    End Sub
End Module

Now assing validation to each textbox...on form loading event

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AssignValidation(Me.TextBox1, ValidationType.Only_Digits)
        AssignValidation(Me.TextBox2, ValidationType.Only_Characters)
    End Sub

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.