WebBrowser1.DocumentCompleted , Error

vmars316

Active member
Joined
Aug 24, 2020
Messages
39
Programming Experience
Beginner
Hello & Thanks ;


Having prob with this statement :
Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

Error code:

GetElementByTag\Form1.vb(7,156): error BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

VB.NET:
 Public Class Form1
    
     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
         WebBrowser1.Navigate(TextBox1.Text)
     End Sub
    
     Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
         Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
    
         For Each CurElement As HtmlElement In PageElements
    
             TextBox2.Text = TextBox2.Text & CurElement.GetAttribute("src") & Environment.NewLine
    
         Next
     End Sub
 End Class

Thanks for your Help...
 
I'm guessing that you have just copied code from the web without knowing what it does. Generally speaking, you would only use that code if you have added a WebBrowser control to your form in the designer named WebBrowser1. That event handler would be generated automatically if you use the appropriate tools in the IDE so it appears that you have not used those tools. If you have a control with a different name then you can change the method declaration by hand but you really ought to have had the IDE generate the event handler for you simply by double-clicking the control in the designer, just as you would for the Click event of a Button. Presumably, your reference to WebBrowser1 inside the method is going to cause an issue too. In simple terms, you can't refer to controls that don't exist.
 
I'm guessing that you have just copied code from the web without knowing what it does. Generally speaking, you would only use that code if you have added a WebBrowser control to your form in the designer named WebBrowser1. That event handler would be generated automatically if you use the appropriate tools in the IDE so it appears that you have not used those tools. If you have a control with a different name then you can change the method declaration by hand but you really ought to have had the IDE generate the event handler for you simply by double-clicking the control in the designer, just as you would for the Click event of a Button. Presumably, your reference to WebBrowser1 inside the method is going to cause an issue too. In simple terms, you can't refer to controls that don't exist.

Hello & Thanks ;
Yes I am a noobie . Coming from PureBasic . I am unable to learn in the traditional ways .
What works for me is to start with an example , and then working my way backwards ,
looking up each statement to see what it is actually doing .
I find this site quite useful
Dot Net Perls

But still haven't found helpful info on available webBrowser1 Events .
I am writing a freeware KidSafeBrowser .
Some of the Events I'll be needing are :
Catch Html request for new page , then checking request aginst a list of Safe_Sites .
Replacing Html element <a> "target _blank " with _self .

But yes I am using Form1,vb and Form1.vb [design] .
I believe , this sub is triggered by sub WebBrowser1_DocumentCompleted Event .
No button needed . I don't know what to do about 'Handles' error ?

Thanks for your Help...
 
I'm guessing that you have just copied code from the web without knowing what it does. Generally speaking, you would only use that code if you have added a WebBrowser control to your form in the designer named WebBrowser1. That event handler would be generated automatically if you use the appropriate tools in the IDE so it appears that you have not used those tools. If you have a control with a different name then you can change the method declaration by hand but you really ought to have had the IDE generate the event handler for you simply by double-clicking the control in the designer, just as you would for the Click event of a Button. Presumably, your reference to WebBrowser1 inside the method is going to cause an issue too. In simple terms, you can't refer to controls that don't exist.

Here is a pic of my designer form , 3 textBox and 1 Button :

DesignerForm-400x250.png

Thanks
 
Here is a pic of my designer form , 3 textBox and 1 Button

That is the problem. 3 Textbox, 1 Button, 0 WebBrowser.

Look at your code again, first at line 3. See how the statement ends "Handles Button1.Click". There is an object "Button1" and an event "Click". Now I bet your "Browse" button is called name is "Button1" in the designer. No error.

Now look at the line where the error appears. No object and an event. The object WebBrowser1 needs to exist for the event to be handled.

Hope this helps.

PS. Welcome to the forum.(y)
 
Last edited:
That is the problem. 3 Textbox, 1 Button, 0 WebBrowser.

Look at your code again, first at line 3. See how the statement ends "Handles Button1.Click". There is an object "Button1" and an event "Click". Now I bet your "Browse" button is called name is "Button1" in the designer. No error.

Now look at the line where the error appears. No object and an event. The object WebBrowser1 needs to exist for the event to be handled.

Hope this helps.

PS. Welcome to the forum.(y)

Thank You very much
benshaws

Ok , I'm off and running , and encourage .

Here is current code :
VB.NET:
Public Class GetElementsByTagName
    Private Sub GetElementsByTagName_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.ScriptErrorsSuppressed = True
        WebBrowser1.Navigate("http://vmars.us/SafeBrowser/SafeBrowserHome.html")

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(TextBox1.Text)
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim PageElements As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")

        For Each CurElement As HtmlElement In PageElements

            TextBox2.Text = TextBox2.Text & CurElement.GetAttribute("src") & Environment.NewLine

        Next
    End Sub

End Class

Thanks
 
Thank You very much
benshaws
Thanks

jmcilhinney deserves all the credit. I just provided a rewrite of post #2. jmcilhinney provided the knowledge and experience to answer your question - not me.

But I am also happy that your are enjoying your first venture into VB.Net coding.:D
 
Back
Top