connectseekers.com

Thursday, 11 September 2014

Restrict User to Enter Only Negative, Non-Negative and Decimal Number in TextBox Using C#.Net in Windows Application

9/11/2014 - By Pranav Singh 2

This article will show you how you can restrict user to enter only numbers and negative number values in text using c#.net in windows application.


So for this article first we will create a new windows application and add the below code into keypress event of the textbox.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnadd_Click(object sender, EventArgs e)
        {

        }
        ///
        /// Validate textbox to enter only number
        ///

        ///
        ///
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            /*This will validate all type of numbers*/
            if ((!char.IsDigit(e.KeyChar)) && !char.IsControl(e.KeyChar) && (e.KeyChar != '-') && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
            /* This will allow only allow minus sign at the beginning*/
            if (e.KeyChar == '-' && (sender as TextBox).Text.Length > 0)
            {
                e.Handled = true;
            }
            /* This will allow only allow one decimal point*/
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }
    }
}

In above code I have first validated weather the added value is number, negative number or decimal number or not. After they it have been validated that user must place negative sign at first place of the number and after that it have been validated that decimal sign added anywhere between numbers and it must be only one time.

So we have done check the output.




About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

2 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top