[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

How do I restrict input to a 4 digit integer in a Windows TextBox in VB.NET?

I'm using a textbox as a year input control, and I want to restrict the input to 4 numeric digits. That is it shouldn't be possible to even key in other characters than 0-9. The input is easy to validate afterwards and provide feedback, but I like the restriction method better.

I have tried to use the MaskedTextBox that holds the functionality, but I really don't like its feel. Is it possible to tweak a MaskedTextbox to have the same feel as a standard TextBox (no prompt characters) or can I in some way implement the above mentioned restriction for a normal TextBox?
0
andreas_rafn
Asked:
andreas_rafn
4 Solutions
 
nepaluzCommented:
Add this to your key_pressevent of the textbox
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        Try
            e.Handled = TrapKey(CStr(Asc(e.KeyChar)))
        Catch ex As Exception
        End Try

    End Sub

Open in new window

And also add this funtion to your form class
    Private Function TrapKey(ByVal KCode As String) As Boolean
        If (CDbl(KCode) >= 48 And CDbl(KCode) <= 57) Then
            TrapKey = False
        Else
            TrapKey = True
        End If

    End Function

Open in new window

0
 
nepaluzCommented:
Or you could add just this to your KeyPress event of the textbox
If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = Chr(Keys.Delete) And Not e.KeyChar = Chr(Keys.Back) And Not e.KeyChar = Chr(45) Then
            e.Handled = True
End If

Open in new window

This allows correction as well as restricting to numbers only. You can then limit the length of the textbox to four in the designer to achieve your overall target.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could use a NumericUpDown() control and set the Minimum() and Maximum() properties:
http://msdn.microsoft.com/en-us/library/57dy4d56.aspx
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
CodeCruiserCommented:
Have you tried using Space as the promptchar in maskedtextbox?
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Everybody forgot something: set the MaxLength property of the control to 4.

The second nepaluz solution is the best because it takes care of letting the backspace go through, something that a lot of programmers forget when they try to limit entry to digits.

I would also add a Console.Beep when the character is rejected, so that the user has a least an idea that he did something wrong.
0
 
andreas_rafnAuthor Commented:
Truly a lot of good suggestions, I went with a combination of MaxLength=4 and a key trapping event handling, which works fine.

As what goes the MaskedTextBox and space as prompt char approach, it just don't provide the same feel as the spaces will affect the cursor location in a weird way.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now