Results 1 to 8 of 8

Thread: [RESOLVED] Webbrowser class problem [VS2005 .NET 2.0]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Resolved [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:
    1. Dim I As Integer = 1
    2.         Dim WB As New WebBrowser
    3.  
    4.         WB.Navigate(Me.URL.Text.Trim.ToString)
    5.  
    6.         If (Not (WB.Document Is Nothing)) Then
    7.             Dim HDoc As HtmlElementCollection = WB.Document.Links
    8.  
    9.             For I = 1 To HDoc.Count
    10.                 Me.PageText.Text = Me.PageText.Text & HDoc.Item(I).ToString & vbCrLf
    11.             Next
    12.         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.

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Webbrowser class problem [VS2005 .NET 2.0]

    try to use this
    VB Code:
    1. Public Class Form1
    2.  
    3.     Dim WithEvents WB As New WebBrowser
    4.  
    5.     Private Sub WB_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
    6.         Dim I As Integer = 1
    7.         If Me.WB.Document.Links.Count <> 0 Then
    8.             'do what ever
    9.         End If
    10.     End Sub
    11. End Class
    Edit **

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Webbrowser class problem [VS2005 .NET 2.0]

    Try this:
    VB Code:
    1. Private Sub getLinks()
    2.         Dim I As Integer = 1
    3.         Dim WB As New WebBrowser
    4.         AddHandler WB.DocumentCompleted, AddressOf DocumentCompleted
    5.         WB.Navigate(Me.URL.Text.Trim.ToString)
    6.     End Sub
    7.     Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)
    8.         Dim wb As WebBrowser = CType(sender, WebBrowser)
    9.         If wb.Document IsNot Nothing Then
    10.             Dim HDoc As HtmlElementCollection = wb.Document.Links
    11.             For Each link As String In HDoc
    12.                 Me.PageText.Text += link + ControlChars.NewLine
    13.             Next
    14.         End If
    15.     End Sub
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Re: Webbrowser class problem [VS2005 .NET 2.0]

    That worked, thank you very much.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Question 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:
    1. Public Function GetPageHrefs(ByVal URL As String) As String()
    2.         Try
    3.             AddHandler WB.DocumentCompleted, AddressOf DocumentReady
    4.             WB.Navigate(URL)
    5.         Catch ex As Exception
    6.             Dim I(1) As String
    7.             I.SetValue(ex.ToString, 1)
    8.             Return I
    9.         End Try
    10.     End Function
    11.  
    12.     Private Function DocumentReady(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) As String()
    13.         Dim WB As System.Windows.Forms.WebBrowser = CType(sender, System.Windows.Forms.WebBrowser)
    14.         If WB.Document IsNot Nothing Then
    15.             Dim HDoc As System.Windows.Forms.HtmlElementCollection = WB.Document.Links
    16.             Return HDoc
    17.         End If
    18.     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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    Question 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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2005
    Location
    Indiana
    Posts
    451

    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
  •  



Click Here to Expand Forum to Full Width