On your textbox's KeyPress Event type this:

       private void yourTextBox_KeyPress(object sender,
KeyPressEventArgs e)
       {
           if (Char.IsNumber(e.KeyChar) )
           {
              // do your stuff if it's a number
           }
           else
           {
               e.Handled = true;          // skip unwanted characters
           }
       }

The code above allows you to accept only numbers, all other characters
are ignored.
As for the input limit of 10 digits only, simply set the MaxLength
property
of the textbox and you're done.




Benj







On Apr 9, 4:23 pm, VIKAS GARG <[email protected]> wrote:
> Hi friends I am developing an application in which there is a text box, in
> this text box I need to have telephone number as an input. So for this I
> need to put a constarint on this text box so that it will only accept
> integers and not other and also there shold be the c ondition so that no
> more than 10 digits could be given....
>
> When I double click on the text box for writing code for the event, the
> event which apears is TextChanged event. and after reading several
> tutoprials on the google about adding such constraint I found that "*
> KeyPress*" event should be used. but when I change this "*TextChange"*  to
> "KeyPress" in  Form1.cs and form1.Designer.cs then also it is not working. I
> have also tried to use regular expression but that also didn't work for me.
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows.Forms;
>
> namespace WindowsApplication10
> {
>   public partial class Form1 : Form
>   {
>   public Form1()
>   {
>   InitializeComponent();
>   }
>
>   private void Form1_Load(object sender, EventArgs e)
>   {
>
>   }
>
>   private void text1_TextChanged(object sender, EventArgs e)
>   {
>
>   }
>   }
>
> }
>
> The above is the code
>
> in my form I have defined only two  fields one is label and  the other is
> text box....
>
> Please help me in putting both the constraint..........

Reply via email to