Solved

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

Posted on 2012-04-04
6
615 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 86

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

In this post we will learn different types of Android Layout and some basics of an Android App.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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…

737 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