Do not allow typing space in maskedTextBox

Asked

Viewed 1,047 times

2

In the windowsforms what is the simplest way to avoid typing spaces into a control maskedTextBox ?

  • What mask are you wearing?

3 answers

3


In the event Keydown of maskedTextBox put the following code below:

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

    Thanks for the help!

1

1

Maybe this will help you help me a lot in my project.

    private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        //não permitir augumas coisas 
        if (char.IsLetter(e.KeyChar) || //Letras

            char.IsSymbol(e.KeyChar) || //Símbolos

            char.IsWhiteSpace(e.KeyChar)) //Espaço

            e.Handled = true; //Não permitir

        // VERIFICAR . - ; entre outros
        if (!char.IsNumber(e.KeyChar) && !(e.KeyChar == ',') && !(e.KeyChar == Convert.ToChar(8)))
        {
            e.Handled = true;
        }

        //só permitir digitar uma vez a virgula
        if (e.KeyChar == ','
            && (sender as TextBox).Text.IndexOf(',') > -1)
        {
            e.Handled = true;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.