My scenario is:
Not allowing spaces at starting position of textbox after enter one or more characters text box allows spaces
Below not applicable to my sce
Try
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.Handled = (e.KeyChar == (char)Keys.Space)) { if(((TextBox)sender).Text.Replace(" ","") == "") { MessageBox.Show("Spaces are not allowed"); ((TextBox)sender).Text = string.Empty; } } }