Search code examples
c#removing-whitespacemaskedtextbox

C# maskedTextbox, how to disable whitespaces?


I cant figure out how to disable whitespaces, I tried multiple things, and yes my mask is 00000000000 but still it allows whitespaces. Anyone know a fix?

Not much code to show, only:

Should only allow numbers to be entered, not whitespaces too :/

enter image description here


Solution

  • Add the KeyDown Event to your textbox and then add the following Code in the created method:

    private void textBox_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Space)
      {
        e.Handled = true;
        e.SuppressKeyPress = true;
        return;
      }
    }