2012-07-11 74 views
1

我知道我似乎問了相同類型的問題,但有很多反覆試驗我最終讓我的應用程序做我需要它做的事情。我有一個表單上有一個按鈕。點擊後,它會創建一個表單,一個Web瀏覽器,一個圖片框,一個文本框和一個按鈕。VB.NET〜我如何封裝我的代碼以反覆使用?

網頁瀏覽器,圖片框,文本框和按鈕都在運行時放置在此表單上。

我的問題是現在我怎麼能封裝這個一遍又一遍的使用。我試過把它放到一個類中並創建這個類的一個實例,但是我保留在文檔上出現錯誤,說明我的代碼並不是指對象。

但這是我的代碼。

感謝

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 
+0

你是什麼意思「一遍又一遍」?你只是想多次調用這些方法..如果是的話,如何使用for循環! – jsp 2012-07-11 20:58:59

回答

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 

的beggining當前範圍(頁)另外,也要看看Namespaces,如果你不知道它是什麼。

+0

感謝蒂亞戈 - 這幫了我很多。 – user1096419 2012-07-15 20:57:13

相關問題