0

Form1 Code:

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    WebBrowser1.Navigate("http://www.----------.org/login.php?")
    WaitForPageLoad()
    WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("login").InvokeMember("Click")

End Sub

the module code:

Private Property pageready As Boolean = False

Region "Page Loading Functions"

Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

End Region

So I was searching around internet for a module that could allow webbrowser1 to loads completely before proceeding next code, I found the code at How to wait until WebBrowser is completely loaded in VB.NET? , but I'm having some trouble with the accessibility, (on the module WebBrowser1.* and main form )it says on that not declared and it may be inaccessible due to its protection level. I'm new to programming I hope someone could help me out.

here's picture links http://s18.postimg.org/8qokcnzh5/Untitled.jpg

Community
  • 1
  • 1

1 Answers1

1

Your protection level error is caused because you must access the function WaitForPageLoad() in your module WaitForPageLoad from Form1's button click event

Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("http://www.----------.org/login.php?")
    WaitForPageLoad.WaitForPageLoad() 'Example: ModuleName.FuncionOrMethodName()
    WebBrowser1.Document.GetElementById("username").SetAttribute("value", TextBox1.Text)
    WebBrowser1.Document.GetElementById("password").SetAttribute("value", TextBox2.Text)
    WebBrowser1.Document.GetElementById("login").InvokeMember("Click")
End Sub
  1. You'll need to make your WaitForPageLoad() public so you can access it from Form1.
  2. You will need to access your WebBrowser1 using Form1 (since your WebBrowser1 is located on your Form1

New WaitForPageLoad()

Public Sub WaitForPageLoad()
    AddHandler Form1.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If Form1.WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler Form1.WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub
Alex
  • 4,821
  • 16
  • 65
  • 106