问题描述:

I am trying to create a program where people can upload text to a database. I am using a WebBrowser to accomplish this however, when the code tries to use the text from the other form it creates a NullReferenceError was unhandled when I try to run the code in debug.

This is my current code in the form when the upload takes place:

Public Class Form2

Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

WebBrowser1.Navigate("http://database.insma.org/")

WebBrowser1.AllowNavigation = True

End Sub

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

WebBrowser1.Document.GetElementById("name").SetAttribute("value", TextBox1.Text)

WebBrowser1.Document.GetElementById("pass").SetAttribute("value", TextBox2.Text)

WebBrowser1.Document.GetElementById("edit-submit--2").InvokeMember("click")

Threading.Thread.Sleep(1000)

WebBrowser1.AllowNavigation = True

WebBrowser1.Navigate("http://database.insma.org/?q=node/add/log")

Threading.Thread.Sleep(1000)

WebBrowser1.Document.GetElementById("title").SetAttribute("value", Form1.TextBox2.Text)

WebBrowser1.Document.GetElementById("field_time_utc_[und][0][value]").SetAttribute("value", Form1.TextBox4.Text)

WebBrowser1.Document.GetElementById("field_location_ex_utwente_hong_k[und][0][value]").SetAttribute("value", Form1.TextBox5.Text)

WebBrowser1.Document.GetElementById("field_frequency[und][0][value]").SetAttribute("value", Form1.TextBox3.Text)

WebBrowser1.Document.GetElementById("field_id[und][0][value]").SetAttribute("value", Form1.TextBox6.Text)

WebBrowser1.Document.GetElementById("field_call_up[und][0][value]").SetAttribute("value", Form1.TextBox7.Text)

WebBrowser1.Document.GetElementById("field_group_count[und][0][value]").SetAttribute("value", Form1.TextBox8.Text)

WebBrowser1.Document.GetElementById("body[und][0][value]").SetAttribute("value", Form1.TextBox1.Text)

WebBrowser1.Document.GetElementById("edit-submit").InvokeMember("click")

Threading.Thread.Sleep(1000)

MsgBox("Uploaded to the database!")

End Sub

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

End Sub

End Class

This is how the error looks like: http://i.imgur.com/LCQQCcy.png

It would be much appreciated if you dumb it down for me when you are explaining what is going on since I may not understand what you guys are trying to do.

It would also be awesome if you supply me with the solution to the error!

Thanks,

Weighted Cube

网友答案:

Just bellow your class Instance, write this:

Public Class Form2
    Dim Form1 As New Form1()
    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.Navigate("http://database.insma.org/")
        WebBrowser1.AllowNavigation = True
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        WebBrowser1.Document.GetElementById("name").SetAttribute("value", TextBox1.Text)
        WebBrowser1.Document.GetElementById("pass").SetAttribute("value", TextBox2.Text)
        WebBrowser1.Document.GetElementById("edit-submit--2").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        WebBrowser1.AllowNavigation = True
        WebBrowser1.Navigate("http://database.insma.org/?q=node/add/log")
        Threading.Thread.Sleep(1000)
        WebBrowser1.Document.GetElementById("title").SetAttribute("value", Form1.TextBox2.Text)
        WebBrowser1.Document.GetElementById("field_time_utc_[und][0][value]").SetAttribute("value", Form1.TextBox4.Text)
        WebBrowser1.Document.GetElementById("field_location_ex_utwente_hong_k[und][0][value]").SetAttribute("value", Form1.TextBox5.Text)
        WebBrowser1.Document.GetElementById("field_frequency[und][0][value]").SetAttribute("value", Form1.TextBox3.Text)
        WebBrowser1.Document.GetElementById("field_id[und][0][value]").SetAttribute("value", Form1.TextBox6.Text)
        WebBrowser1.Document.GetElementById("field_call_up[und][0][value]").SetAttribute("value", Form1.TextBox7.Text)
        WebBrowser1.Document.GetElementById("field_group_count[und][0][value]").SetAttribute("value", Form1.TextBox8.Text)
        WebBrowser1.Document.GetElementById("body[und][0][value]").SetAttribute("value", Form1.TextBox1.Text)
        WebBrowser1.Document.GetElementById("edit-submit").InvokeMember("click")
        Threading.Thread.Sleep(1000)
        MsgBox("Uploaded to the database!")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)

    End Sub
End Class
相关阅读:
Top