Only allow a Year Range in textbox control

  • Thread starter Thread starter Gerry Viator
  • Start date Start date
G

Gerry Viator

Hi all,

I would like only a year in a range from 1950 to 2040 to be alowed in a
textbox control.

This doesn't seam to be working...

Private Sub txtboxYear_KeyDown(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyEventArgs) Handles txtboxYear.KeyDown

Dim strKey As String
strKey = e.KeyCode.ToString().Remove(0,
e.KeyCode.ToString().Length - 1)

If txtboxYear.Text <> Nothing Then
Dim FirstNumb As String
Dim SecondNumb As String
Dim ThirdNumb As String
Dim FourthNumb As String

If txtboxYear.Text.Length = 1 Then
If strKey = "1" Or strKey = "2" Then
Else
e.Handled = True
End If

ElseIf txtboxYear.Text.Length = 2 Then
If strKey = "0" Or strKey = "9" Then
Else
e.Handled = True
End If

ElseIf txtboxYear.Text.Length = 3 OrElse txtboxYear.Text.Length
= 4 Then
If strKey > "0" And strKey <= "9" Then
Else
e.Handled = True
End If

End If

Else

If strKey = "1" Or strKey = "2" Then
Else
e.Handled = True
End If
End If

End Sub



can someone help thanks
Gerry
 
Hi Gerry,

Thanks for your post. I reviewed your code carefully, and now I'd like to
share the following information with you:

1. Based on my experience, you will need to check the value in the KeyPress
event handler instead of KeyDown, when the text of TextBox control will be
updated.

2. I recommend you covert to and compare in integer values in order to
validate a year range because of it extensibility. I implement the
following algorithmn for your reference:

Private Sub txtboxYear_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles txtboxYear.KeyPress

Dim nTempYear As Integer
Dim nTxtLen As Integer
Dim nMinYear As Integer = 1950
Dim nMaxYear As Integer = 2040

If e.KeyChar > "9" Or e.KeyChar < "0" Then
e.Handled = True
Else

If txtboxYear.Text = Nothing Then
nTempYear = 0
Else
nTempYear = Int32.Parse(txtboxYear.Text)
End If

nTempYear = nTempYear * 10 + Int32.Parse(e.KeyChar)
nTxtLen = txtboxYear.Text.Length

For n As Integer = 4 - nTxtLen - 1 To 1 Step -1
nMinYear = nMinYear \ 10
nMaxYear = nMaxYear \ 10
Next n

If nTempYear < nMinYear Or nTempYear > nMaxYear Then
e.Handled = True
End If
End If
End Sub

Please check it on your side and let me know the result.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Thanks for your help,

Doesn't allow me to type any number in the textbox?

Thanks
Gerry
 
I changed the top to this:

If Not e.KeyChar.IsControl(e.KeyChar) AndAlso _
(e.KeyChar < "0" Or e.KeyChar > "9") Then

e.Handled = True

That seamed to fix that problem, but
I can type the correct numbers, but get an error if I use backspace
to retype a year.

Thanks
Gerry
 
Ok,

I think I fixed that problem as well.

This is what I changed in the code you sent

If Not e.KeyChar.IsControl(e.KeyChar) AndAlso _
(e.KeyChar < "0" Or e.KeyChar > "9") Then
e.Handled = True

Else

If e.KeyChar = ControlChars.Back Then 'Allows backspace
Exit Sub
End If
........
.......


thanks
Gerry
 
Back
Top