vb.net VB 2010 Microsoft.VisualBasic.dll 中发生了“System.InvalidCastException”类型的第一次机会异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19621846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:38:31  来源:igfitidea点击:

VB 2010 A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

vb.netvisual-studio-2010visual-studiovisual-studio-2012

提问by user2925725

I'm new to programming in Visual Basic 2010. I've got these error:

我是 Visual Basic 2010 编程的新手。我遇到了以下错误:

A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Microsoft.VisualBasic.dll 中发生了“System.InvalidCastException”类型的第一次机会异常

Here's my code.

这是我的代码。

Public Class Form1

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
    If TextBox2.ReadOnly = True Then
        TextBox2.ReadOnly = False
    End If
End Sub

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

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If TextBox3.ReadOnly = False Then
        TextBox3.ReadOnly = True
        TextBox3.Text = "|n/a|"
    End If
    If TextBox2.ReadOnly = False Then
        TextBox2.ReadOnly = True
    End If
End Sub

  Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
    TextBox3.Text = "[1.5.2]"
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If CheckBox1.Checked Then
        Button1.Enabled = False
        Threading.Thread.Sleep(500)
        Button2.PerformClick()
        Button3.PerformClick()
        Button4.PerformClick()
        Threading.Thread.Sleep(500)
        If RichTextBox1.Text = Nothing Then
            Threading.Thread.Sleep(5000)
            Button3.PerformClick()
            Button3.PerformClick()
            Label4.Text = "Next try in 10 seconds"
            Threading.Thread.Sleep(10000)
            Label4.Text = "Trying...."
            Button1.Enabled = True
            Button1.PerformClick()
        End If
    End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    TabControl1.SelectedTab = TabPage2
    Threading.Thread.Sleep(1000)
    WebBrowser2.Navigate("https://login.minecraft.net?user=" + TextBox1.Text + "&password=" + TextBox2.Text + "&version=13")

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    RichTextBox1.Text = WebBrowser2.DocumentText
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    If WebBrowser2.DocumentText.Contains("Bad login") Then
        MessageBox.Show("Bad login!")
    End If
    If WebBrowser2.DocumentText.Contains("User not premium") Then
        MessageBox.Show("User not premium! Please buy Minecraft at the http://minecraft.net/ ")
    End If
    If RichTextBox1.Text.Contains("Bad login") Then
        MessageBox.Show("Bad login!")
    End If
    If RichTextBox1.Text.Contains("User not premium") Then
        MessageBox.Show("User not premium! Please buy Minecraft at the http://minecraft.net/ ")
    End If
End Sub

End Class

回答by Matthias

An InvalidCastExceptionmeans that you tried to handle an object as something other, what it's not able to be. For instance, you can't treat a string like an integer. I'd argue that it's related to the TabPage2. I don't see any declaration for this.

AnInvalidCastException意味着您试图将一个对象作为其他对象来处理,这是它无法成为的。例如,您不能将字符串视为整数。我认为它与TabPage2. 我没有看到任何声明。