Pergunta

In C#, WinForms how would you stop the following inputs:

0000000000001234556

0000.123456

00000123456.123456

Foi útil?

Solução

If you want 1234556 instead of 000000001234556
Then you can convert it in Int,Double or any other Datatype as you want

2nd option is use the keypress event

private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        if (e.KeyCode == ascii of 0)
        {
            e.Handled = false;
        }
    }

Outras dicas

You can override the KeyPress event and define a state that, if input is == '0' then ignore, else, wait for a non-zero input. Then change the state to accept 0s.

Since you do not show code, I will not either

You could add an event handler to the KeyDown or KeyPress events. Inside the event handler check if the KeyEventArgs KeyCode is 0 and if the length of your TextBox.Text property is 0. If so set the KeyEventArgs Handled property to false.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top