vb.net由于其保护级别可能无法访问

时间:2013-07-14 13:34:25

标签: vb.net login error-handling browser webbrowser-control

Form1代码:

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

模块代码:

Private Property pageready As Boolean = False

区域“页面加载功能”

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

结束地区

所以我在互联网上搜索一个模块,可以让webbrowser1在继续下一个代码之前完全加载,我在How to wait until WebBrowser is completely loaded in VB.NET?找到了代码,但我在访问性方面遇到了一些麻烦,(在模块上) WebBrowser1。*和主表单)它表示未声明,并且由于其保护级别而可能无法访问。 我是编程新手,希望有人能帮助我。

这是图片链接 http://s18.postimg.org/8qokcnzh5/Untitled.jpg

1 个答案:

答案 0 :(得分:1)

您的保护级别错误是由于您必须从Form1的按钮单击事件访问模块中的函数WaitForPageLoad() WaitForPageLoad

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. 您需要将WaitForPageLoad() 公开,以便您可以从 Form1 访问它。
  2. 您需要使用 Form1 访问 WebBrowser1 (因为您的WebBrowser1位于您的Form1上
  3. 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