Solved

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

Posted on 2012-04-04
6
612 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
ID: 37808479
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
ID: 37808499
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
ID: 37808741
You could use a NumericUpDown() control and set the Minimum() and Maximum() properties:
http://msdn.microsoft.com/en-us/library/57dy4d56.aspx
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 50 total points
ID: 37808878
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
ID: 37814423
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
ID: 37819007
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

776 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