Problem searching for zip code in Windows Forms

Asked

Viewed 80 times

1

I added in a Windows Forms form the option to return the address through the zip code. I used a web service to obtain this data and added in the following method:

private void ConsultaCEP()
    {
        var valor = txtCep.Text;

        try
        {
            var ws = new WSCorreios.AtendeClienteClient();
            var resposta = ws.consultaCEP(valor);

            txtLogradouro.Text = resposta.end;
            txtBairro.Text = resposta.bairro;
            txtCidade.Text = resposta.cidade;
            cbxUf.Text = resposta.uf;
        }
        catch (Exception ex)
        {
            lblAvisoCep.Visible = true;
            lblAvisoCep.Text = "Erro ao efetuar busca";
            txtCep.BackColor = Color.LemonChiffon;
            txtCep.Clear();
            txtCep.Focus();
        }
    }

And I called this method in the event Leave from the zip code textbox.

 private void txtCep_Leave_1(object sender, EventArgs e)
    {
        ConsultaCEP();
    } 

However, when typing an invalid zip code, the error message appears but the form hangs, and it is not possible to change the cursor to any field other than the zip code. How to solve this problem?

  • 1

    Every time you try to get out of txtCep he fires the event txtCep_Leave_1 that creates an error and inside it has a txtCep.Focus();. I don’t know how they usually do, but I would use the typing event and check the amount of characters, when it was the quantity related to the zip code I would call the consultation service.

  • Remove the txtCep.Focus() that almost certainly won’t happen.

  • If you don’t find a zip you play an Exception?

  • 1

    I’m voting to close because the author never returned to add clarifications or confirm if he managed to solve the problem.

No answers

Browser other questions tagged

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