Pergunta

Wasn't sure what to title this as and couldn't find anything regarding my particular problem.

i have the following snippet of code which allows me to backspace text entered into an EditText box.

btnClear.Click += delegate {
            nView.Text = nView.Text.Remove(nView.Text.Length - 1, 1);
        };

When i eventually clear the EditText box and click once more (going to -1 characters), the app crashes and says 'Cannot Be Negative'.

I need some kind of prevention to stop users from deleting too many characters and crashing the app.

What do i implement? I'm using C# as my coding language too people. Thanks.

Foi útil?

Solução

You simply need to add an "if":

btnClear.Click += delegate {
    if (nView.Text.Length > 0)
        nView.Text = nView.Text.Remove(nView.Text.Length - 1, 1);
};
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top