Skip to content


Only Numbers in TextBox

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Requirement: TextBox in C# should accept only numbers as input.

Solution:

Raise an event KeyPress and paste following line of code.

private void txtInput_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsNumber(e.KeyChar))
e.Handled = true;
}

Here, e stands for the arguments of the function and e contains several attributes and methods like:

e.KeyChar : Character that has been pressed
e.Handled : Whether it is an acceptable character or not.


Related Posts:

Posted in ASP.NET, C#, Microsoft .NET.

Tagged with , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.