Saturday, August 31, 2013

How do make the texbox accept only numerics, del key and backspace?


public partial class Form1 : Form
{
/// <summary>
/// Whether Del is pressed or not
/// </summary>
private bool isDelPressed = false;

public Form1()
{
InitializeComponent();
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
int n = (int)e.KeyChar;
e.Handled = !(n == 8 || (n >= 48 && n <= 57) || isDelPressed);
}

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
isDelPressed = (e.KeyCode == Keys.Delete);
}
}



If you think one reply solves your problem, please mark it as An Answer , if you think someone's reply helps you, please mark it as a Proposed Answer



Help by clicking:

Click here to donate your rice to the poor

Click to Donate

Click to feed Dogs & Cats




Found any spamming-senders? Please report at: Spam Report


No comments:

Post a Comment