473,385 Members | 1,630 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Only allow a Year Range in textbox control

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
Jul 21 '05 #1
4 1624
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.

Jul 21 '05 #2
Thanks for your help,

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

Thanks
Gerry
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:Kq**************@cpmsftngxa06.phx.gbl...
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.

Jul 21 '05 #3
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
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:Kq**************@cpmsftngxa06.phx.gbl...
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.

Jul 21 '05 #4
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
"Tian Min Huang" <ti******@online.microsoft.com> wrote in message
news:Kq**************@cpmsftngxa06.phx.gbl...
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.

Jul 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
2
by: Brian P. Bailey | last post by:
I would like to: 1] Output messages to a user in a scrollable control 2] Allow the user to copy selected text from this control 3] The user should be prevented from modifying the text in any way...
9
by: subdhar | last post by:
I'm getting following error in asp.net application.I search the web and couldn't find error like this can any one help me in trouble with this error Specified argument was out of the range of...
8
by: tfsmag | last post by:
i need to create a date range based on the current "shown" month on a calendar control to query a database and populate a datagrid based on that date range. how can i retrieve the "shown" month...
16
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
4
by: Gerry Viator | last post by:
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,...
20
by: Highlander | last post by:
Hello all. Consider the following HTA: <html> <head> <title>Date Pulldowns</title> <HTA:APPLICATION ID="HTAUI" APPLICATIONNAME="Date Pulldowns" SCROLL="no" SINGLEINSTANCE="yes"
5
by: royend | last post by:
Is it possible to hide images from the internet, and only have a image available for users that are logged into the intranet? I am hoping to avoid a database-solution as the number of images will...
23
by: Dan Tallent | last post by:
A textbox has a attribute for ReadOnly. This seems like such a simple concept. When a textbox is set to read only the user cannot change the contents of the field. I have been trying to find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.