Problems with Message Dialog in the Key Down event

Asked

Viewed 90 times

3

I put an event KeyDown in a field TextBox for when the user presses Enter, there must be some action.

In this action, I’ve asked for a message to appear Dialog, but the problem is that if the user presses twice fast the key Enter, the message will appear twice on the screen and this will cause the system to lock.

Is there a solution for this? how to block the enter key when the user is typing for the second time while it is already open? or another solution?

Press Enter on "Average Score":

inserir a descrição da imagem aqui

Dialog:

public async Task mensagemSimples(TextBox text, string titulo, string mensagem)
{
    var metroWindow = (Application.Current.MainWindow as MetroWindow);
    await metroWindow.ShowMessageAsync(titulo, mensagem);
    text.Focus();
    text.SelectAll();
}

When I call her:

private async void textRespPontuacaoMed_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            int min = Convert.ToInt32(textRespPontuacaoMin.Text);
            int med = Convert.ToInt32(textRespPontuacaoMed.Text);

            if (med < min)
            {
                await mensagemSimples(textRespPontuacaoMed, "Atenção", "Pontuação Média deve ser maior que a Pontuação Mínima");
                e.Handled = false;
            }else
                textRespPontuacaoMax.Focus();
        }
    }

Thanks.

1 answer

1

I would just wait for the user to interact and then switch to Focus.

private async void textRespPontuacaoMed_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        int min = Convert.ToInt32(textRespPontuacaoMin.Text);
        int med = Convert.ToInt32(textRespPontuacaoMed.Text);

        if (med < min)
        {
            await mensagemSimples(textRespPontuacaoMed, "Atenção", "Pontuação Média deve ser maior que a Pontuação Mínima");
            text.Focus();
            text.SelectAll();
            e.Handled = false;
        }else
            textRespPontuacaoMax.Focus();
    }
}

  public async Task mensagemSimples(TextBox text, string titulo, string mensagem)
{
    var metroWindow = (Application.Current.MainWindow as MetroWindow);
    await metroWindow.ShowMessageAsync(titulo, mensagem);

}

Browser other questions tagged

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