Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want that user should enter only numbers in the text box not alphabets. how can i check that user enter only number in the textbox programatically. or how can I stop users to enter alphabets in the text box. I will appreciate your help.
Posted
Comments
Nish Nishant 9-Feb-11 12:34pm    
Winforms or WPF?

Since you have tagged it as VB.NET, I assume you are taking of Winforms.

Now, all you need is to tie a OnKeyDown event to the textbox. In code behind, at runtime, whenever a key is pressed, OnKeyDown method would be invoked. In it, check for the key stroke and accordingly decide if you want to allow it or not.

If needed, have a look at similar discussion here[^].
 
Share this answer
 
Comments
juli Paris 9-Feb-11 12:35pm    
Thank you sir, This simple thing will solve my problem.
Sandeep Mewara 9-Feb-11 12:38pm    
Good to know. :)
Henry Minute 9-Feb-11 12:49pm    
If someone helps you to solve a problem you should vote them a 5 as well as marking their answer as the solution.

It's only polite to do so. :)
Sergey Alexandrovich Kryukov 9-Feb-11 12:52pm    
All right, my 5, but how to make them Google, Google..?
Poor Internet gets blotted by repeated answers like that, going to blow soon...
--SA
Sandeep Mewara 9-Feb-11 12:54pm    
:-)

Just felt, OP sounded orignal and needed. So, all good. :)
hi,
Better think is u can try to make a usercontrol. because if validate that think(only numeric) in onkeydown( this is also wrong, use onkeypress) then you must write that validation code in every textbox keypress. But you will make a user control for numeric textbox
then u can use any ware in your project. this is only good. and its easy also.

just add a new item, choose user control and drag a text box then write this code http://msdn.microsoft.com/en-us/library/ms229644.aspx . finally build your project, this textbox will come on toolbox. jst use and enjoy
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900