Ejemplo n.º 1
0
        private void txtCep_TextChanged(object sender, EventArgs e)
        {
            string cepText = txtCep.Text.ToString();

            try {
                int inputSize = txtCep.Text.ToString().Length;

                if (inputSize == 9)
                {
                    string cep = cepText;
                    cep = Basics.limpaCep(cep);

                    string api_url = "https://api.postmon.com.br/v1/cep/" + cep;

                    string   request = Basics.httpGet(api_url);
                    Endereco result  = JsonConvert.DeserializeObject <Endereco>(request);

                    if (!String.IsNullOrEmpty(result.Logradouro))
                    {
                        txtAddress.Text      = result.Logradouro;
                        txtNeighborhood.Text = result.Bairro;
                        txtCity.Text         = result.Cidade;
                        txtState.Text        = result.EstadoInfo.Nome;

                        txtAddress.ReadOnly = txtNeighborhood.ReadOnly = txtCity.ReadOnly = txtState.ReadOnly = true;
                    }
                }
                else
                {
                    txtAddress.Text = txtNeighborhood.Text = txtCity.Text = txtState.Text = "";

                    txtAddress.ReadOnly = txtNeighborhood.ReadOnly = txtCity.ReadOnly = txtState.ReadOnly = false;
                }
            } catch (Exception ex) {
                Console.WriteLine("Erro: " + ex.Message);
            }
        }