Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2012-04-04
6
Medium Priority
?
624 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 1200 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 200 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 200 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 400 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

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

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

610 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