Selecionar texto em VB

Pestrela

Membro
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>"

:cool:
 
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>"

:cool:

Dá lá um exemplo do texto que vais buscar e como vais buscar.
 
o texto e obtido da seguinte forma

Private Sub ir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ir.Click

browser.Navigate(url.Text)

End Sub


Private Sub browser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
urltxt.Text = browser.DocumentText
End Sub

que me vai mostrar o codigo html da pagina numa textbox


O que eu queria era que em vex de aparecer o codigo html todo, apenas fosse mostrado os tags html "<a href>" e o seu conteudo.
 
isto é mais vb6 mas da para perceber a ideia ;)
faz assim:

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

'...
atençao que nao testei e o maisprovavel é ter um bug qualquer. :D

edit: ta cheio de bugs
edit2: tem um loop infinito hehehe :D
 
Última edição:
ou seja entre "<a href" e "</a>" é isso ?

Só mais uma coisa tem caracteres especiais correcto ?
 
Última edição pelo moderador:
isto é mais vb6 mas da para perceber a ideia ;)
faz assim:

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

'...
atençao que nao testei e o maisprovavel é ter um bug qualquer. :D

edit: ta cheio de bugs
edit2: tem um loop infinito hehehe :D


Eu sou mm verde na programação em VB ainda :(

Será que m podias dar sugestoes para alterar o ciclo infinito?
 
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
 
n existe um texto especifico

o codigo pode ser retirado de kkr pagina por exemplo.. Eu tenho um botão e uma textbox que servem para usar o browser

Private Sub ir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ir.Click

browser.Navigate(url.Text)

End Sub
Depois quando o brower carrega a pagina tenho uma linha de codigo que mostra o codigo fonte da pagina numa textbox


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
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>"
 
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



Só mostra o ultimo <a href="..">..</a> :D

E já viste o exemplo que mostrei ... ? (pelo que vejo é só fazer umas pequenas adaptações)


Sim ja exprimentei. Muito obrigado :D

Vou tentar fazer as modificações com os meus "grandes conhecimentos" na matéria :p

a alteração que fix foi a seguinte

Código:
Dim str As String = browser.DocumentText
De kkr das formas o meu unico problema neste exemplo é o facto de so aparecer a ultima tag :(
 
Última edição pelo moderador:
Testaste o meu exemplo ? Experimenta alterar Debug.WriteLine(tagname) para MessageBox.Show(tagname) e diz-me se aparece só a ultima tag.
 
Testaste o meu exemplo ? Experimenta alterar Debug.WriteLine(tagname) para MessageBox.Show(tagname) e diz-me se aparece só a ultima tag.

Dessa forma aparecem msgboxes sem fim :p

O meu objectivo era que as tags aparecessem numa textbox mas s puser

Código:
textbox1.text=tagname

apenas aparece a ultima tag :(
 
A mensagem era só para veres, se quiseres colocar numa textbox podes fazer:

Código:
[SIZE=2][COLOR=#0000ff][COLOR=white]me[/COLOR][/COLOR][/SIZE][SIZE=2][COLOR=white].TextBox1.AppendText(tagname)[/COLOR]
[/SIZE]
Não te esqueças do multiline a da scrollbar.

Dessa forma aparecem msgboxes sem fim :p

O meu objectivo era que as tags aparecessem numa textbox mas s puser

Código:
textbox1.text=tagname
apenas aparece a ultima tag :(

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.
 
Última edição pelo moderador:
Back
Topo