I want to validate the textbox like , the textbox can not accept string value..i can accept only the number value..............................

At first i had validated the textbox in VB.Net like :

=========================================================================================Private Sub txt1_KeyPress(KeyAscii As Integer)
    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 8 Then
        KeyAscii = 0
    End If

    If KeyAscii = 8 Then
        KeyAscii = 8
    End If
End Sub
=========================================================================================

But i have no idea , how to validate textbox in C#.Net.I have tried but does not works..

=========================================================================================
private void txt1_KeyPress(object sender, KeyPressEventArgs e)
{
            int a = e.KeyChar;

            if((a<48 || a>57) && a!=8)
            {
                // ???????????????????????            
            }
}

Personally, I find the easiest way to restrict the user to numeric entry is to use a NumericUpDown control.
This already has all the key checking built-in.
You can also specify a maximum and minimum value to further validate the entry before processing.

If you still wish to use the Textbox then the code snippet you posted should work fine.
You just need to indicate that the kepress has been processed.

private void txt1_KeyPress(object sender, KeyPressEventArgs e)
        {
            int a = e.KeyChar;
            if ((a < 48 || a > 57) && a != 8)
            {
                e.Handled = true; //<-- indicate that the key has been processed
            }
        }
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.