Boas
Tou a fazer um programa e precisava d ajuda
O meu programa funciona cmo browser e vai a kkr pagina da net e retira-lhe o codigo htlm todo para uma textbox.
Gostava de saber cmo fazer para que apenas aparecesse na text box as linhas d codigo que comecem por "<a href>"
dim ...
'...
Dim html As String, tempHtml As String
Dim posIni As Variant, posFim As Variant
html = browser.documentText
tempHtml = ""
posIni = InStr(html, "<a href>")
posFim = InStr(html, "</a>")
While posIni <> 0
posIni = InStr(posIni, html, "<a href>", 1)
posFim = InStr(posIni, html, "</a>", 1)
tempHtml = tempHtml & Mid(html, posIni, posFim - posIni)
Wend
Me.Text2.Text = tempHtml
'...
ou seja entre "<a href" e "</a>" é isso ?
isto é mais vb6 mas da para perceber a ideia
faz assim:
atençao que nao testei e o maisprovavel é ter um bug qualquer.Código:dim ... '... Dim html As String, tempHtml As String Dim posIni As Variant, posFim As Variant html = browser.documentText tempHtml = "" posIni = InStr(html, "<a href>") posFim = InStr(html, "</a>") While posIni <> 0 posIni = InStr(posIni, html, "<a href>", 1) posFim = InStr(posIni, html, "</a>", 1) tempHtml = tempHtml & Mid(html, posIni, posFim - posIni) Wend Me.Text2.Text = tempHtml '...
edit: ta cheio de bugs
edit2: tem um loop infinito hehehe
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String = "isto é um teste <a href='vbtuga'>vbtuga.blogspot.com</a> isto " + _
"é um segundo teste<a href='techzonept'>http://www.techzonept.com/</a>fim do teste ." + _
"isto é um teste <a href='vbtuga'>vbtuga.blogspot.com</a> isto é um segundo " + _
" teste<a href='techzonept'>http://www.techzonept.com/</a>fim do teste"
Dim lastPosition As Integer
' Check the textbox information
Dim findPrefix As String = "<a href="
Dim findSufix As String = "</a>"
Try
Do
' Find the sufix in the line
If str.Substring(lastPosition).Contains(findPrefix) Then
' Collects the end position
Dim posStart As Integer = str.IndexOf(findPrefix, lastPosition) + findPrefix.Length
' Breaks the str for prefix search
Dim strFound As String = str.Substring(posStart + findPrefix.Length)
If strFound.Contains(findSufix) Then
' Collects the end position
Dim posEnd As Int16 = strFound.IndexOf(findSufix) + findSufix.Length
' Collects the tag
Dim tagname As String = str.Substring(posStart, posEnd + findSufix.Length).ToString
lastPosition = (posStart + posEnd)
Debug.WriteLine(tagname)
Else
Exit Do
End If
Else
Exit Do
End If
Loop
Catch ex As Exception
End Try
End Sub
Depois quando o brower carrega a pagina tenho uma linha de codigo que mostra o codigo fonte da pagina numa textboxPrivate Sub ir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ir.Click
browser.Navigate(url.Text)
End Sub
O que pretendo é que ao invés de aparecer o codigo fonte todo, só fosse mostrado o que estivesse nas tags "<a href = "www.sitequalquer.qualquercoisa">CENAS ESCRITAS</a>"Private Sub browser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browser.DocumentCompleted
urltxt.Text = browser.DocumentText
End Sub
Bem não tendo algo para testar aqui vai uma ideia
Código:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str As String = "isto é um teste <a href='vbtuga'>vbtuga.blogspot.com</a> isto " + _ "é um segundo teste<a href='techzonept'>http://www.techzonept.com/</a>fim do teste ." + _ "isto é um teste <a href='vbtuga'>vbtuga.blogspot.com</a> isto é um segundo " + _ " teste<a href='techzonept'>http://www.techzonept.com/</a>fim do teste" Dim lastPosition As Integer ' Check the textbox information Dim findPrefix As String = "<a href=" Dim findSufix As String = "</a>" Try Do ' Find the sufix in the line If str.Substring(lastPosition).Contains(findPrefix) Then ' Collects the end position Dim posStart As Integer = str.IndexOf(findPrefix, lastPosition) + findPrefix.Length ' Breaks the str for prefix search Dim strFound As String = str.Substring(posStart + findPrefix.Length) If strFound.Contains(findSufix) Then ' Collects the end position Dim posEnd As Int16 = strFound.IndexOf(findSufix) + findSufix.Length ' Collects the tag Dim tagname As String = str.Substring(posStart, posEnd + findSufix.Length).ToString lastPosition = (posStart + posEnd) Debug.WriteLine(tagname) Else Exit Do End If Else Exit Do End If Loop Catch ex As Exception End Try End Sub
E já viste o exemplo que mostrei ... ? (pelo que vejo é só fazer umas pequenas adaptações)
Dim str As String = browser.DocumentText
Testaste o meu exemplo ? Experimenta alterar Debug.WriteLine(tagname) para MessageBox.Show(tagname) e diz-me se aparece só a ultima tag.
textbox1.text=tagname
[SIZE=2][COLOR=#0000ff][COLOR=white]me[/COLOR][/COLOR][/SIZE][SIZE=2][COLOR=white].TextBox1.AppendText(tagname)[/COLOR]
[/SIZE]
Dessa forma aparecem msgboxes sem fim
O meu objectivo era que as tags aparecessem numa textbox mas s puser
apenas aparece a ultima tagCódigo:textbox1.text=tagname
Aparece a ultima porque em cada uma que encontras escreves na textbox. A ultima vez que escreves é que fica visível. Tens de usar o AppendText.