Public
Class Form1
Private Sub BuscarCEP()
Me.Cursor
= Cursors.WaitCursor
If Me.MkdCEP.MaskCompleted Then
Me.CEP(Me.MkdCEP.Text)
Me.txtNumeroRua.Focus()
Else
MsgBox("Por favor informe o CEP para
que possa localizar o endereço.", MsgBoxStyle.Exclamation)
End If
Me.Cursor
= Cursors.Default
End Sub
''' <summary>
''' Método que retorna um endereço com base no CEP (faz uma
busca na internet)
''' </summary>
Protected Sub CEP(ByVal pCEP As String)
Dim ds As DataSet
Dim
Resultado As String
= String.Empty
Dim
DD(3) As String
Try
ds = New
DataSet()
ds.ReadXml("http://cep.republicavirtual.com.br/web_cep.php?cep="
+ pCEP.Replace("-", "").Trim() + "&formato=xml")
If Not IsNothing(ds) Then
If
(ds.Tables(0).Rows.Count > 0) Then
Resultado =
ds.Tables(0).Rows(0).Item("resultado").ToString()
Select Case Resultado
Case "1"
DD(0) =
(ds.Tables(0).Rows(0).Item("uf").ToString().Trim())
DD(1) = (ds.Tables(0).Rows(0).Item("cidade").ToString().Trim())
DD(2) =
(ds.Tables(0).Rows(0).Item("bairro").ToString().Trim())
DD(3) =
(ds.Tables(0).Rows(0).Item("logradouro").ToString().Trim())
Case "2"
DD(0) =
(ds.Tables(0).Rows(0).Item("uf").ToString().Trim())
DD(1) =
(ds.Tables(0).Rows(0).Item("cidade").ToString().Trim())
DD(2) = String.Empty
DD(3) = String.Empty
Case Else
DD(0) = String.Empty
DD(1) = String.Empty
DD(2) = String.Empty
DD(3) = String.Empty
End
Select
End
If
End
If
Me.CmbUF.Text
= DD(0)
Me.txtCidade.Text
= DD(1)
Me.txtBairro.Text
= DD(2)
Me.txtRua.Text
= DD(3)
Catch
ex As Exception
MessageBox.Show("Falha ao Buscar o Cep" & vbCrLf
& ex.Message & vbCrLf & ex.Source)
End Try
End Sub
Private Sub btnLocalizar_Click(sender As System.Object, e As System.EventArgs)
Handles btnLocalizar.Click
Me.BuscarCEP()
End Sub
Private Sub mkdCEP_KeyPress(sender As
Object, e As
System.Windows.Forms.KeyPressEventArgs) Handles
MkdCEP.KeyPress
If
Asc(e.KeyChar) = Keys.Enter Then
Me.BuscarCEP()
End If
End Sub
End Class
Pegue o projeto neste link: http://www.mediafire.com/?tfy5nkt8d4pmd38
Até mais abraços...
0 comentários:
Postar um comentário