TextBox Accepting Only Numbers in Windows Forms

In this post, we will learn about how to create TextBox Accepting Only Numbers in Windows Forms with an example.

Description:

In my previous posts I have explained various topics like Get site visitor count in asp.net
Convert generic list to datatable in Asp.Net C#.Net, Abstract Class in C# with Example,
Routing in Asp.Net MVC with Example
,Use of Global.asax in Asp.net


Now in this post, I will explain about creating TextBox Accepting Only Numbers in Windows Forms with appropriate example.

Many a timesyou come across a situation wherein you want to allow only Numbers in textbox for ex. Count of member, Count of dishes etc. You can make use of Keypress event of textbox in winform to check which key is pressed and accordngly you can prevent any other key pressed by user.
Now create Windows Form Application in Visual Studio and write below lines of code in it.

private void txtRate_KeyPress(object sender, KeyPressEventArgs e)
{
            try
            {
                e.Handled = !char.IsDigit(e.KeyChar); // Does not allow backspace                
            }           
}

If you want to allow backspace and numeric then use below lines of codes.

private void txtRate_KeyPress(object sender, KeyPressEventArgs e)
{
            try
            {
                e.Handled = !char.IsDigit(e.KeyChar); // This line allow numeric and backspace                
            }           
}




Read :

Summary

You can also read about ASP.NET, C#.Net, JQUERY, AJAX, JavaScript
I hope you get an idea about TextBox Accepting Only Numbers in Windows Forms.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you liked this post, don’t forget to share this.

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *
Email *
Website