textBox1_KeyPress (Question)

C

Claudia Fong

Hi,

I would like to control the user's input; In a textBox where the user is
only allow to put 0-9 (eg telephone number) I want to show a messagebox
if a user inputs a character like a-z..

Can someone help me with this?

Here's the code I started with, but don't know how to finish..

private void textBox1_KeyPress(object
sender,System.Windows.Forms.KeyPressEventArgs e)
{
string ex;
if(e.KeyChar ---> this will get the input's char.
MessageBox.Show("ERROR");
}



Cheers!

Claudi
 
D

Dmytro Lapshyn [MVP]

Hi,

if(!Char.IsDigit(e.KeyChar))
{
MessageBox.Show("ERROR");
e.Handled = true;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top