VB.NET~如何封装我的代码以便反复使用?

时间:2012-07-11 20:48:28

标签: vb.net browser encapsulation

我知道我似乎问过相同类型的问题,但经过多次尝试和错误,我终于让我的应用程序做了我需要做的事情。我有一个带有按钮的表单。单击时,它会创建表单,Web浏览器,图片框,文本框和按钮。

网页浏览器,图片框,文本框和按钮都在运行时放在此表单上。

我现在的问题是如何将其封装起来以便一次又一次地使用。我试过把它放到一个类中并创建一个类的实例,但是我在文档上遇到错误,说明我的代码并不是指对象。

但这是我的代码。

由于

Public Class CaptchaWindow
Dim myform As New Form
Dim webbrowser As New WebBrowser
Dim picturebox As New PictureBox
Dim textbox As New TextBox
Dim submitbtn As New Button
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ProfileMaker.Show()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call myspecs()


AddHandler submitbtn.Click, AddressOf captchasubmit
AddHandler webbrowser.DocumentCompleted, AddressOf webcompleteddesignfloat
webbrowser.Navigate("http://www.designfloat.com/register/")

End Sub

Sub myspecs()


'FORM SIZE AND LOCATION
myform.Size = New Drawing.Size(1542, 771)
myform.Location = New Drawing.Point(15, 15)

'WEBBROWSER SIZE AND LOCATION
webbrowser.Size = New Drawing.Size(1000, 577)
webbrowser.Location = New Drawing.Point(12, 181)
webbrowser.ScriptErrorsSuppressed = True

'PICTUREBOX SIZE AND LOCATION
picturebox.Size = New Drawing.Size(299, 83)
picturebox.Location = New Drawing.Point(12, 12)
picturebox.BorderStyle = BorderStyle.Fixed3D


'TEXBOX SIZE AND LOCATION
textbox.Size = New Drawing.Size(299, 20)
textbox.Location = New Drawing.Point(12, 101)

'BUTTON SIZE AND LOCATION
submitbtn.Size = New Drawing.Size(75, 23)
submitbtn.Location = New Drawing.Point(118, 127)
submitbtn.Text = "Submit"

myform.Controls.Add(webbrowser)
myform.Controls.Add(picturebox)
myform.Controls.Add(textbox)
myform.Controls.Add(submitbtn)
myform.Show()




End Sub

Sub webcompleteddesignfloat()
For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_username" Then
element.SetAttribute("value", ProfileMaker.Username.Text)
End If
Next

For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_email" Then
element.SetAttribute("value", ProfileMaker.Email.Text)
End If
Next

For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next

For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("name") = "reg_password2" Then
element.SetAttribute("value", ProfileMaker.Password.Text)
End If
Next

If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?    c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If


End Sub

Sub captchasubmit()

If webbrowser.ReadyState = WebBrowserReadyState.Complete Then
For Each Captcha As HtmlElement In webbrowser.Document.Images
If Captcha.GetAttribute("src").Contains("http://www.google.com/recaptcha/api/image?c=") Then
picturebox.Load(Captcha.GetAttribute("src"))
End If
Next
End If

For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("input")
If element.GetAttribute("id") = "recaptcha_response_field" Then
element.SetAttribute("value", textbox.Text)
End If
Next

For Each element As HtmlElement In webbrowser.Document.GetElementsByTagName("button")
If element.GetAttribute("name") = "submit" Then
element.InvokeMember("click")
End If
Next


myform.Dispose()

End Sub

以下部分是我尝试从类中创建此实例的时候。

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
mystuff.Handler()
mystuff.captchasubmit()
mystuff.webcompleteddesignfloat()


End Sub
End Class

1 个答案:

答案 0 :(得分:0)

你应该Imports你的类命名空间到代码开头的当前范围(页面)

Imports Namespace.CaptchaWindow

Class Page1


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim mystuff As New CaptchaClass
End Sub

End Class

如果您不知道它是什么,请查看Namespaces