Solved

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

Posted on 2012-04-04
6
610 Views
Last Modified: 2012-08-13
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
Comment
Question by:andreas_rafn
6 Comments
 
LVL 17

Expert Comment

by:nepaluz
Comment Utility
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
 
LVL 17

Accepted Solution

by:
nepaluz earned 300 total points
Comment Utility
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
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
Comment Utility
You could use a NumericUpDown() control and set the Minimum() and Maximum() properties:
http://msdn.microsoft.com/en-us/library/57dy4d56.aspx
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 50 total points
Comment Utility
Have you tried using Space as the promptchar in maskedtextbox?
0
 
LVL 40

Assisted Solution

by:Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger) earned 100 total points
Comment Utility
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
 

Author Closing Comment

by:andreas_rafn
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is about my first experience with programming Arduino.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now