|
-
Nov 3rd, 2006, 04:41 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Webbrowser class problem [VS2005 .NET 2.0]
Hello,
I am having a problem with some code I putting together. I am trying to implement a webbrowser class, without using the webbrowser control on a form. I want to navigate to a page, determine if the page is loaded, then extract the link collection and display them in a text box on a form.
Here is my code.
VB Code:
Dim I As Integer = 1
Dim WB As New WebBrowser
WB.Navigate(Me.URL.Text.Trim.ToString)
If (Not (WB.Document Is Nothing)) Then
Dim HDoc As HtmlElementCollection = WB.Document.Links
For I = 1 To HDoc.Count
Me.PageText.Text = Me.PageText.Text & HDoc.Item(I).ToString & vbCrLf
Next
End If
The problem I am having is that it says the WB.Document is nothing. Am I missing something? I am just learning .NET and this is the first time doing this, so I am not sure what I am missing. I have looked at MSDN, and done some googling, but don't see anything similar to what I am doing. Everyone else tries to use regex, but I think this would be better, if I can figure out how to do it.
Thank you for your help.
Last edited by Krenshau; Nov 4th, 2006 at 12:39 PM.
Reason: needed further assistance.
-
Nov 3rd, 2006, 04:51 PM
#2
Re: Webbrowser class problem [VS2005 .NET 2.0]
try to use this
VB Code:
Public Class Form1
Dim WithEvents WB As New WebBrowser
Private Sub WB_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
Dim I As Integer = 1
If Me.WB.Document.Links.Count <> 0 Then
'do what ever
End If
End Sub
End Class
Edit **
Last edited by VBDT; Nov 3rd, 2006 at 04:55 PM.
-
Nov 3rd, 2006, 04:59 PM
#3
Re: Webbrowser class problem [VS2005 .NET 2.0]
Try this:
VB Code:
Private Sub getLinks()
Dim I As Integer = 1
Dim WB As New WebBrowser
AddHandler WB.DocumentCompleted, AddressOf DocumentCompleted
WB.Navigate(Me.URL.Text.Trim.ToString)
End Sub
Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
Dim wb As WebBrowser = CType(sender, WebBrowser)
If wb.Document IsNot Nothing Then
Dim HDoc As HtmlElementCollection = wb.Document.Links
For Each link As String In HDoc
Me.PageText.Text += link + ControlChars.NewLine
Next
End If
End Sub
"I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
My Blog
-
Nov 3rd, 2006, 05:43 PM
#4
Thread Starter
Hyperactive Member
Re: Webbrowser class problem [VS2005 .NET 2.0]
That worked, thank you very much.
-
Nov 3rd, 2006, 11:36 PM
#5
Thread Starter
Hyperactive Member
Re: [RESOLVED] Webbrowser class problem [VS2005 .NET 2.0]
Thank you for helping me before. I need some more help, though.
I am trying to get the adapt the code from above to be used in a class. I think it is possible, but I can't figure out how to do this. I think I have to convert this collection that is returned into an array, then pass that array out of the function to the other function, then out of that function to the calling client.
Here is my code.
VB Code:
Public Function GetPageHrefs(ByVal URL As String) As String()
Try
AddHandler WB.DocumentCompleted, AddressOf DocumentReady
WB.Navigate(URL)
Catch ex As Exception
Dim I(1) As String
I.SetValue(ex.ToString, 1)
Return I
End Try
End Function
Private Function DocumentReady(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) As String()
Dim WB As System.Windows.Forms.WebBrowser = CType(sender, System.Windows.Forms.WebBrowser)
If WB.Document IsNot Nothing Then
Dim HDoc As System.Windows.Forms.HtmlElementCollection = WB.Document.Links
Return HDoc
End If
End Function
If someone can point me in the right direction on doing this, I would be grateful. Also, can someone tell me if the way I shoot the error message out in the first function will work?
Thank you for your help.
-
Nov 3rd, 2006, 11:47 PM
#6
Re: [RE-OPENED] Webbrowser class problem [VS2005 .NET 2.0]
If you don't want to display a Web page then you shouldn't be using the WebBrowser control at all. Use the WebClient class instead, which doesn't have the overhead of the control that you will never make use of anyway. The WebClient.DownloadString method will retrieve the source code for a Web page to a String object, which you can then parse to extract the parts you need. If you want just the links then there are numerous examples about of using regular expressions to do so.
-
Nov 4th, 2006, 12:09 AM
#7
Thread Starter
Hyperactive Member
Re: [RE-OPENED] Webbrowser class problem [VS2005 .NET 2.0]
Thank you for your suggestion, but I don't want to use regular expression. This is more accurate. I have used regular expressions, and they are great for many things, but I prefer the webbrowser for this purpose, I just haven't used it in a class situation where I had to move the data around like this. I don't care about overhead in this situation, either.
Also, I am going to be creating different functions to extract many different attributes, not just the href. They will use this same pattern, once I figure out the method.
Last edited by Krenshau; Nov 4th, 2006 at 12:13 AM.
-
Nov 4th, 2006, 12:39 PM
#8
Thread Starter
Hyperactive Member
Re: [RE-OPENED] Webbrowser class problem [VS2005 .NET 2.0]
I am setting this to resolved. I have to rethink how this will work because the sub that gets called finishes after it passes the url to the webbrowser, this doesn't allow the data from the document complete to be passed back to the client. Since I can't use an even to pass it back to the client, I will have to rethink this.
Thank you for your help.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|