Hi,

How can i restrict the user to input only numbers.

I have done the following coding for VB

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (!char.IsNumber(e.KeyChar))

            {

                e.Handled = true;

            }
}

But now i used it in C# it is not showing the (e.keychar) property.

I have to change my code but i dont know insted of keychar which property work in C#

Recommended Answers

All 4 Replies

use validators will easy

The text changed event arguments dont include "e.KeyChar" because the event is not always fired by a keypress.
If you use the Textbox.KeyPress event, the event args will include the KeyChar value to show which key fired the event.
It should be ntoed at this point that the KeyPress event is not a perfect solution to input validation; if the user PASTES text into the textbox it wont fire the keypress event for the values entered so they could paste invalid text.

If you want to validate the text in the TextChanged event you will need to check the TextBox.Text property. I'd recomend looking at the Regex class for this, its a very powerful string matching tool.

Alternatively, if this is an ASP.NET web project you can use the Validation controls. Or you could throw out everything i said here and use a NumericUpDown control which only allows numbers by default :)

Thank u all

Its working i wrote the program on text change event that why i dont find the property keychar.

thank u

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.