how to restrict textbox to number/textbox numbers only/textbox just numbers
  • Hi my title is complete question i have textbox in C# 2008 and i want that user just insert number and dont allow alpha bate or alphanumeric too.Kindly provide easy and short peace of code. 
  • 2 Comments sorted by
  • Vote Up0Vote Down WaqasWaqas
    172.00 Karma Accepted Answer
    Hi use KeyPress Event for this work code is below first will allow decimal point "." and second will not allow point too.


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    //code allow to user insert number with decimal point like 44.4
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
    {
    e.Handled = true;
    }

    // only allow one decimal point
    if (e.KeyChar == '.'
    && (sender as TextBox).Text.IndexOf('.') > -1)
    {
    e.Handled = true;
    }
    }

    and other option is this.


    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
    //only allow just numbers like 0123456789 without decimal point "."
    if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
    {
    e.Handled = true;
    }
    }
  • Vote Up0Vote Down studentstudent
    146.00 Karma Accepted Answer
    Good job

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Tagged

Popular Posts of the Week

Optimum Creative