PC Review


Reply
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average.

Re: Restrict keys in textbox w/o KeyAscii?

 
 
hansolo
Guest
Posts: n/a
 
      28th May 2004
> Ok folks. Had some code in VB6 that worked fine within a TextBox KeyPress
> events KeyAscii parameter for limiting the text in a text box to only
> numbers, backspace and decimal characters. I understand the whole VB.Net
> situation about not being able to assign anything other than zero (no key
> stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress event
> so I have rolled my own function as suggested by Microsoft to handle
> limiting key strokes in a TextBox.
>
> The Function:
>
> Function RestrictTxt(ByVal keyz As Integer) As Boolean
> If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
> RestrictTxt = False
> Else
> RestrictTxt = True
> End If
> End Function
>
> The TextBox KeyPress Event using the function:
>
> Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
> eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

Text2.KeyPress
> Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
> eventArgs.Handled = RestrictTxt(KeyAscii)
> End Sub
>
> The Problem:
>
> It basically does what its supposed to except for two strange errors.
> 1) When I press the "n" key which should have a key code value of 78, the
> function is getting passed the value 110 which should be the Del (decimal)
> key. As my function above allows the value 110, it passes False to
> eventArgs.Handled and somehow the letter "n" is allowed in the textbox.
>
> 2) The only other key I am having trouble with is the "/". When I press

the
> "/" key (keypad or main keys) which should have a key code value of 111,

the
> function is getting passed the value 47 which should be the HELP key. As

my
> function above allows values 46 thru 57, it passes False to
> eventArgs.Handled and somehow the character "/" is allowed in the textbox.
>
> If you know how these keys "n" and "/" are evaluating to the incorrect key
> code values in my code but resulting in the correct character (which I

don't
> want) in the text box please let me know. If possible please try on your
> system as I am thinking something is wrong with my keyboard :-). In the
> second case, I can easily edit the function to exclude value 47. It was
> included to make the code simpler by using a range but in the first case,

I
> cannot exclude value 110 because I need the decimal character.
>
> Thanks,
>
> Chuck
>
>
>



 
Reply With Quote
 
 
 
 
Claes Bergefall
Guest
Posts: n/a
 
      28th May 2004
Can't seem to find the start of this thread?!
Well, ayway

There is nothing wrong with your keyboard
You have your ASCII codes mixed up
n = 110
N = 78
Backspace = 8
/ = 47

ASCII for Del is irrelevant since it doesn't generate a KeyPress event
Never heard of the HELP key. Do you mean F1?
You should support ',' aswell (ASCII 44)

/claes

"hansolo" <(E-Mail Removed)> wrote in message
news:vYytc.201$eP.39@lakeread01...
> > Ok folks. Had some code in VB6 that worked fine within a TextBox

KeyPress
> > events KeyAscii parameter for limiting the text in a text box to only
> > numbers, backspace and decimal characters. I understand the whole

VB.Net
> > situation about not being able to assign anything other than zero (no

key
> > stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress

event
> > so I have rolled my own function as suggested by Microsoft to handle
> > limiting key strokes in a TextBox.
> >
> > The Function:
> >
> > Function RestrictTxt(ByVal keyz As Integer) As Boolean
> > If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
> > RestrictTxt = False
> > Else
> > RestrictTxt = True
> > End If
> > End Function
> >
> > The TextBox KeyPress Event using the function:
> >
> > Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
> > eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

> Text2.KeyPress
> > Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
> > eventArgs.Handled = RestrictTxt(KeyAscii)
> > End Sub
> >
> > The Problem:
> >
> > It basically does what its supposed to except for two strange errors.
> > 1) When I press the "n" key which should have a key code value of 78,

the
> > function is getting passed the value 110 which should be the Del

(decimal)
> > key. As my function above allows the value 110, it passes False to
> > eventArgs.Handled and somehow the letter "n" is allowed in the textbox.
> >
> > 2) The only other key I am having trouble with is the "/". When I press

> the
> > "/" key (keypad or main keys) which should have a key code value of 111,

> the
> > function is getting passed the value 47 which should be the HELP key.

As
> my
> > function above allows values 46 thru 57, it passes False to
> > eventArgs.Handled and somehow the character "/" is allowed in the

textbox.
> >
> > If you know how these keys "n" and "/" are evaluating to the incorrect

key
> > code values in my code but resulting in the correct character (which I

> don't
> > want) in the text box please let me know. If possible please try on

your
> > system as I am thinking something is wrong with my keyboard :-). In the
> > second case, I can easily edit the function to exclude value 47. It was
> > included to make the code simpler by using a range but in the first

case,
> I
> > cannot exclude value 110 because I need the decimal character.
> >
> > Thanks,
> >
> > Chuck
> >
> >
> >

>
>



 
Reply With Quote
 
 
 
 
hansolo
Guest
Posts: n/a
 
      28th May 2004
Thanks, you are correct as I am sure you know. I was getting my information
from the Keys Enumeration member help topic in .NET 2003. Evedently a few
things in this table do not match the Ascii codes. Pretty close though.

Thanks for picking up on my error.

Chuck


"Claes Bergefall" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Can't seem to find the start of this thread?!
> Well, ayway
>
> There is nothing wrong with your keyboard
> You have your ASCII codes mixed up
> n = 110
> N = 78
> Backspace = 8
> / = 47
>
> ASCII for Del is irrelevant since it doesn't generate a KeyPress event
> Never heard of the HELP key. Do you mean F1?
> You should support ',' aswell (ASCII 44)
>
> /claes
>
> "hansolo" <(E-Mail Removed)> wrote in message
> news:vYytc.201$eP.39@lakeread01...
> > > Ok folks. Had some code in VB6 that worked fine within a TextBox

> KeyPress
> > > events KeyAscii parameter for limiting the text in a text box to only
> > > numbers, backspace and decimal characters. I understand the whole

> VB.Net
> > > situation about not being able to assign anything other than zero (no

> key
> > > stroke) to the eventArgs.KeyChar parm in the VB.Net TextBox KeyPress

> event
> > > so I have rolled my own function as suggested by Microsoft to handle
> > > limiting key strokes in a TextBox.
> > >
> > > The Function:
> > >
> > > Function RestrictTxt(ByVal keyz As Integer) As Boolean
> > > If keyz >= 46 And keyz <= 57 Or keyz = 8 Or keyz = 110 Then
> > > RestrictTxt = False
> > > Else
> > > RestrictTxt = True
> > > End If
> > > End Function
> > >
> > > The TextBox KeyPress Event using the function:
> > >
> > > Private Sub Text2_KeyPress(ByVal eventSender As System.Object, ByVal
> > > eventArgs As_ System.Windows.Forms.KeyPressEventArgs) Handles

> > Text2.KeyPress
> > > Dim KeyAscii As Integer = Asc(eventArgs.KeyChar)
> > > eventArgs.Handled = RestrictTxt(KeyAscii)
> > > End Sub
> > >
> > > The Problem:
> > >
> > > It basically does what its supposed to except for two strange errors.
> > > 1) When I press the "n" key which should have a key code value of 78,

> the
> > > function is getting passed the value 110 which should be the Del

> (decimal)
> > > key. As my function above allows the value 110, it passes False to
> > > eventArgs.Handled and somehow the letter "n" is allowed in the

textbox.
> > >
> > > 2) The only other key I am having trouble with is the "/". When I

press
> > the
> > > "/" key (keypad or main keys) which should have a key code value of

111,
> > the
> > > function is getting passed the value 47 which should be the HELP key.

> As
> > my
> > > function above allows values 46 thru 57, it passes False to
> > > eventArgs.Handled and somehow the character "/" is allowed in the

> textbox.
> > >
> > > If you know how these keys "n" and "/" are evaluating to the incorrect

> key
> > > code values in my code but resulting in the correct character (which I

> > don't
> > > want) in the text box please let me know. If possible please try on

> your
> > > system as I am thinking something is wrong with my keyboard :-). In

the
> > > second case, I can easily edit the function to exclude value 47. It

was
> > > included to make the code simpler by using a range but in the first

> case,
> > I
> > > cannot exclude value 110 because I need the decimal character.
> > >
> > > Thanks,
> > >
> > > Chuck
> > >
> > >
> > >

> >
> >

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
restrict the user entering nonalphanumeric keys? =?Utf-8?B?Qw==?= Microsoft ASP .NET 1 13th Jun 2007 11:31 PM
HELP! I Lost The Ability To Advance From TextBox To TextBox With the ENTER Or The TAB Keys Minitman Microsoft Excel Programming 0 22nd Feb 2005 09:50 PM
Is it possible to restrict the use of USB Keys ONLY ? Lars Microsoft Windows 2000 Group Policy 1 16th Dec 2003 09:29 AM
Re: Is it possible to restrict the use of USB Keys ONLY ? Buz [MSFT] Microsoft Windows 2000 Group Policy 0 15th Dec 2003 05:43 PM
restrict entry in a textbox to a range of values Dillonstar Microsoft Excel Programming 5 30th Oct 2003 06:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:28 AM.