Do not allow space in maskedTextBox

2

No windowsforms what is the simplest way to avoid typing spaces in a maskedTextBox control?

    
asked by anonymous 16.12.2014 / 12:47

3 answers

3

In the KeyDown event of the 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;
        }
    }
    
16.12.2014 / 12:54
1

Another way to use the KeyPress event, why?

The KeyPress Event will trigger while the user is pressing the key, unlike the KeyDown.

private void maskedTextBox1_KeyPress(object sender, KeyEventArgs e) {
        e.Handled = e.KeyChar == ' ';
}

Source: link

    
16.12.2014 / 13:03
1

Maybe this will help you 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;
        }
    }
    
16.12.2014 / 18:04