windows form textbox numbers only 1

windows form textbox numbers only

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
    {
        MessageBox.Show("Please enter only numbers.");
        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}

Here is what the above code is Doing:
1. The textBox1_TextChanged event is called whenever the text in the textbox changes.
2. The Regex.IsMatch method is used to check if the text in the textbox matches the regular expression.
3. If the text matches the regular expression, then the textbox is cleared and a message is displayed.

Similar Posts