No windowsforms
what is the simplest way to avoid typing spaces in a maskedTextBox
control?
No windowsforms
what is the simplest way to avoid typing spaces in a maskedTextBox
control?
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;
}
}
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
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;
}
}