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?
Every time you try to get out of
txtCep
he fires the eventtxtCep_Leave_1
that creates an error and inside it has atxtCep.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.– Barbetta
Remove the
txtCep.Focus()
that almost certainly won’t happen.– João Martins
If you don’t find a zip you play an Exception?
– Leandro Angelo
I’m voting to close because the author never returned to add clarifications or confirm if he managed to solve the problem.
– Leandro Angelo