Was wondering, is it possible to create a class which has the majority of your keypress controls?
Like allowing just letters or numbers in a textbox etc....

To make things clearer what i exactly want.

I have a textbox on my form.
This is a textbox for the postcode.
The postcode is supposed be something along the lines of AA 1234.
This will be checked on lostfocus.

But i need to code this in a class, and this is where i am stuck.

Recommended Answers

All 6 Replies

Your best bet would be to use a regular expression. Either clientside via javascript or serverside the regular expression would look something like this.

[A-Z]{2}\s[0-0]{4}

In C# like this

Regex exp = new Regex("[A-Z]{2}\s[0-0]{4}");

And javascript

var exp = /[A-Z]{2}\s[0-0]{4}/
exp.test();

Hmm thats not really what i am using, thanks though.

This is a example i got, but it has to much useless code.
And i am trying to find a simplified version.

This is the code from the textbox on form1

Private Sub TxbPostcod_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxbPostcode.LostFocus

        If Con.Postcode(TxbPostcode.Text) Then
        Else
            MsgBox(" Postcode not OK, needs to have 2 letters and 4 numbers seperated by a space.: EB 3698")
            TxbPostcode.Text = ""
            TxbPostcode.Select()
        End If

And this is the class, which holds the code to check the textbox on the form.

Public Class Controle

    Public Function Postcode(ByVal Number As String) As Boolean

        Dim Correct As Boolean = True
        Dim strdeel1 As Char
        Dim strdeel2 As String
        Dim ASCII1 As Integer
        Dim Digit As Integer

        If Number = "" Then
            Correct = False
            Return Correct
        End If

        strdeel1 = Microsoft.VisualBasic.Left(Number, 1)
        strdeel2 = Microsoft.VisualBasic.Right(Number, 4)

        If Not IsNumeric(strdeel2) Then
            Correct = False
            Return Correct 
               End If

        Digit = Microsoft.VisualBasic.Right(Nummer, 5)

        If Digit < 1000 Or Digit > 9999 Then
            Correct = False
        End If


        ASCII1 = Convert.ToInt32(strdeel1)

        If ASCII1 > 90 Or ASCII1 < 65 Then
            Correct = False
        End If

        strdeel1 = Microsoft.VisualBasic.Mid(Number, 2, 1)

        ASCII1 = Convert.ToInt32(strdeel1)

        If ASCII1 > 90 Or ASCII1 < 65 Then
            Correct = False
        End If

        strdeel1 = Microsoft.VisualBasic.Mid(Number, 3, 1)

        ASCII1 = Convert.ToInt32(strdeel1)

        If Not ASCII1 = 32 Then
            Correct = False
        End If

        Return Correct

    End Function

End Class

As i explained in my first post, the idea is to have a class which controls a textbox.
The textbox should only accept inputs like DE 3214.
The example is working, but it has to much useless crap in it, and i want a more cleaner way.

Was wondering, is it possible to create a class which has the majority of your keypress controls?
Like allowing just letters or numbers in a textbox etc....

To make things clearer what i exactly want.

I have a textbox on my form.
This is a textbox for the postcode.
The postcode is supposed be something along the lines of AA 1234.
This will be checked on lostfocus.

But i need to code this in a class, and this is where i am stuck.

Hi,

When you says that you need to do it in a Class means for me that you need to create a Custom TextBox right.

You can find an example (Checkbox) how to create a custom Control, here.

If however it isn't really necessary you can try a MaskedTextBox.
Try this:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        MaskedTextBox1.Mask = "LL 0000"
    End Sub

    Private Sub MaskedTextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.LostFocus
        If MaskedTextBox1.MaskFull Then
            MaskedTextBox1.BackColor = Color.Tomato
            If MaskedTextBox1.MaskCompleted Then

                MessageBox.Show("Entry in mask is valid", "Valid Entry", MessageBoxButtons.OK, MessageBoxIcon.Information)

            End If
        Else
            MaskedTextBox1.BackColor = Color.White
        End If
    End Sub
End Class

Well its a regular textbox.
But the control code is in a different class(project-> add class)

Like the example i got, the code from my previous post.
The big part from the class.
This is what checks the textbox on my form if it has 2 letters and 4 numbers separated by a space.(Example DE 1234)

And the small piece of code Private Sub TxbPostcod_LostFocus is whats refers to the code in my class.

Hope that makes sense.

See if this helps.

Public Class Form1

    Private Sub TxbPostcode_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TxbPostcode.LostFocus
        Dim txtValidator As New txtValidation
        If Not txtValidator.validateTextBox(TxbPostcode) = True Then
            MsgBox("Postcode not OK." & vbNewLine & " Needs to have 2 letters and 4 numbers seperated by a space.: EB 3698", MsgBoxStyle.Critical)
            TxbPostcode.Clear() : TxbPostcode.Select()
        End If
    End Sub
End Class

Public Class txtValidation
    Public Function validateTextBox(ByVal selectedTextBoxToValidate As TextBox) As Boolean
        With selectedTextBoxToValidate
            If .Text = "" Then Return False '// if no text.
            If Not .TextLength = 7 Then Return False '// if not correct length.
            If Not Char.IsLetter(.Text(0)) OrElse Not Char.IsLetter(.Text(1)) Then Return False '// if not first 2 char. are letters.
            If Not .Text(2) = " " Then Return False '// if not 3rd char. is space.
            '// if not last 4 char. are #'s.
            If Not Char.IsDigit(.Text(3)) OrElse Not Char.IsDigit(.Text(4)) OrElse Not Char.IsDigit(.Text(5)) OrElse Not Char.IsDigit(.Text(6)) Then Return False
            '// if not less/greater than amount set.
            If CInt(.Text.Substring(3)) < 1000 OrElse CInt(.Text.Substring(3)) > 9999 Then Return False
        End With
        Return True '// if all cleared.
    End Function
End Class

Ok that works, thanks for helping me out.
Makes it alot easier to understand the whole control.

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.