p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   .NET Framework 1.x (http://p2p.wrox.com/forumdisplay.php?f=203)
-   -   ASCII code on keypress event (http://p2p.wrox.com/showthread.php?t=43414)

ashokkumar June 2nd, 2006 08:46 PM

ASCII code on keypress event
 
Hai
I have a text box in which when a key is pressed, i have to check if it is a number or not. The idea i had was to check with ASCII codes for numbers. if anyone Could provide a solution for thsi problem it would be great of them.
Ash

ashok

jbenson001 June 5th, 2006 12:24 PM

Is this a windows or web app?


Shyam Bharath June 8th, 2006 04:00 AM

VB.NET

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

        If e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57) Then
            MsgBox(e.KeyChar.ToString)
        End If

End Sub

ashokkumar June 8th, 2006 05:43 AM

It is a Web Application and the code

  If e.KeyChar >= Chr(48) And e.KeyChar <= Chr(57) Then
            MsgBox(e.KeyChar.ToString)
        End If
isnt working
 do i have to import any name spaces
Thanks and regards
ashok

ashok

jbenson001 June 8th, 2006 08:46 AM

You need to use javascript for this. Are you just trying to validate that the user only enters a numeric value? If so, use the compare validator.


Shyam Bharath June 8th, 2006 11:53 PM

Hi...

The previous code was for WindowsApplication

This one is for Web Application

Anyway, let us assume that u are taking input in a textbox in a web app. First set AutoPostBack to True

Then wirte the following code in the event OnTextChanged

Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Dim str As String
        str = TextBox1.Text
        If Not IsNumeric(str) Then
            Response.Write("Enter a Number !!! ")
            TextBox1.Focus()
        End If
End Sub

On Losing focus OR On submitting, it will check it a number is entered. Or alternately as Benson suggested, use a CompareValidator
Regards

ashokkumar June 9th, 2006 03:09 AM

Hi,
Shyam Bharath

Thanks for the code. This code is getting executed only if an event occurs on the page. This is not happening on keypress. Is thare any way to handle on key press.Thanks for the reply. It would be great to read again from you.
Ashok

ashok

Shyam Bharath June 18th, 2006 09:37 AM

I am sorry...There is unfortunately no event handler either in .net 1.x or in .net 2.x for that

This might be because ecmascript does not support keypress in standard HTML textboxes. We may have to wait for some future version of ecmascript to support that.

---------------
The .NET Underground Portal
www.pureportals.com/developers
---------------

rhd110 March 22nd, 2007 09:05 AM

Is there a similar funtion in C#.net i.e., like IsNumeric or Not IsNumeric

Please Help with C# Windows Programming code


vinay_shrivastava03 March 29th, 2007 07:50 AM

I want to search
how to print variable on form VB.NET


All times are GMT -4. The time now is 05:23 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.