Reputation: 269
I want to restrict the user from entering space in textbox here in my code it simply gets the first input then check if it is space. What I want to do is in the whole text the user can't enter space in textbox
private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
{
if ((sender as TextBox).SelectionStart == 0)
e.Handled = (e.KeyChar == (char)Keys.Space);
else
e.Handled = false;
}
Upvotes: 0
Views: 192
Reputation: 762
you need to use textbox changed event for prevent copy paste white spaces
private void txtPassword_TextChanged(object sender, EventArgs e)
{
if (txtPassword.Text.Contains(" "))
{
txtPassword.Text = txtPassword.Text.Replace(" ", "");
txtPassword.SelectionStart = txtPassword.Text.Length;
}
}
Upvotes: 1