I want my textbox to never accept special chars. Only accepts space,numbers and letters. I found this code for Presskey event:
private void rsNameTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = e.KeyChar != (char)Keys.Back && !char.IsSeparator(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar);
}
But it doesnt work when someone paste something in the textbox. How can I make a textChanged event equivalent?
I tried replacing the not accepted chars for "" with this function but its not working.Its showing any chars when I paste and for some reason its erasing the default initial text "text1":
private void rsNameTextBox_TextChanged(object sender, EventArgs e)
{
Regex reg = new Regex(@"^[\s\dA-Za-z]+$");
rsNameTextBox.Text = reg.Replace(rsNameTextBox.Text,"");
}