2013-07-15 166 views
1

所以我得到這個錯誤,說我得到一個System.invalidOperationexception 這裏是完整的錯誤:的Visual Basic System.invalidOperationexception

[Managed to Native Transition] 
Port Scan.exe!WindowsApplication1.My.MyProject.MyForms.Form1.get() 
Port Scan.exe!WindowsApplication1.My.MyApplication.OnCreateMainForm() 
    Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicatio Base.OnRun() 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication Base.DoApplicationModel() 
Microsoft.VisualBasic.dll!Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplication Base.Run(string[] commandLine) 
[Native to Managed Transition] 
mscorlib.dll!System.Runtime.Hosting.ApplicationActivator.CreateInstance(System.ActivationCo ntext activationContext, string[] activationCustomData) 
    Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.H ostProc.RunUsersAssemblyDebugInZone() 
    mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContex t executionContext, System.Threading.ContextCallback callback, object state, bool  preserveSyncCtx) 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) 
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext  executionContext, System.Threading.ContextCallback callback, object state) 
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() 
[Native to Managed Transition] 

,這裏是我試圖編譯代碼,它的端口掃描儀

Public Class Form1 
Dim host As String 

Dim counter As Integer 
Dim portmin As Integer = TextBox3.Text 
Dim portmax As Integer = TextBox2.Text 
Private Sub Form1_Load(ByVal sender As System.Object, _ 
      ByVal e As System.EventArgs) Handles MyBase.Load 
    Button1.Enabled = False 
    'set counter explained before to 0 
    counter = 0 
End Sub 
Private Sub Timer1_Tick(ByVal sender As System.Object, _ 
     ByVal e As System.EventArgs) Handles Timer1.Tick 
    'Set the host and port and counter 
    counter = counter + 1 'counter is for the timer 
    host = TextBox1.Text 



    For port As Integer = portmin To portmax 

     If (port = portmax) Then 
      Exit For 
     End If 



     ' Next part creates a socket to try and connect 
     ' on with the given user information. 

     Dim hostadd As System.Net.IPAddress = _ 
      System.Net.Dns.GetHostEntry(host).AddressList(0) 
     Dim EPhost As New System.Net.IPEndPoint(hostadd, port) 
     Dim s As New System.Net.Sockets.Socket(_ 
     System.Net.Sockets.AddressFamily.InterNetwork, _ 
    System.Net.Sockets.SocketType.Stream, _ 
     System.Net.Sockets.ProtocolType.Tcp) 
     Try 
      s.Connect(EPhost) 
     Catch 
     End Try 
     If Not s.Connected Then 
      ListBox1.Items.Add("Port " + port.ToString + " is not open") 
     Else 
      ListBox1.Items.Add("Port " + port.ToString + " is open") 
      ListBox2.Items.Add(port.ToString) 

     End If 
     Label3.Text = "Open Ports: " + ListBox2.Items.Count.ToString 
    Next 
End Sub 

Private Sub Button1_Click(ByVal sender As System.Object, _ 
      ByVal e As System.EventArgs) Handles Button1.Click 
    'stop button 
    Timer1.Stop() 
    Timer1.Enabled = False 
    Button1.Enabled = True 
    Button2.Enabled = False 
End Sub 

Private Sub Button2_Click(ByVal sender As System.Object, _ 
    ByVal e As System.EventArgs) Handles Button2.Click 
    ListBox1.Items.Add("Scanning: " + TextBox1.Text) 
    ListBox1.Items.Add("-------------------") 
    Button2.Enabled = True 
    Button1.Enabled = False 
    Timer1.Enabled = True 
    Timer1.Start() 
End Sub 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 

End Sub 

Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged 

End Sub 

Private Sub ListBox2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox2.SelectedIndexChanged 

End Sub 
End Class 

我真的很感激你的幫助,因爲我一直在這個錯誤卡住了一段時間,不能建立exe。看來只有當我執行一個for循環以允許最小和最大端口號時纔會出錯。

+0

'portmin'('TextBox3.Text')和'portmax'('TextBox2.Text')的值是什麼? –

回答

4

的問題是在此代碼:

Dim portmin As Integer = TextBox3.Text 
Dim portmax As Integer = TextBox2.Text 

在這些語句執行的時間,形式並不完全構造呢。 TextBox2TextBox3尚未創建。您正試圖將參考Nothing分配給一個整數值。這是不允許的。將分配移至表單的Load事件。也花時間檢查並確保這些框的內容將轉換爲整數。

+0

不太確定「將它移動到Load事件」是個好建議。用戶仍然沒有時間輸入值。而且更糟糕的失敗模式,Load事件處理程序中的異常往往會被吞噬。最好使用「開始」按鈕。 –

+0

感謝您的超快速回答 – Gabe

+0

@Gabe - 同樣,進入您的項目屬性並打開Option Strict On或將Option Strict On放在代碼的頂部。 –