"The call stack contains only external code" 错误

"The call stack contains only external code" error

有人可以帮我吗?事情 运行 很顺利,但突然间,调试停止然后它引导我到这个 -> error

更糟糕的是,我试图在一个新项目中重新进行整个设计和编码,认为该问题只出现在那个特定项目中。浪费了大概两个小时。我将我的整个代码放在这里,以防问题出在我的代码中,这会很长,但请帮助我。我可能漏掉了什么。

Imports System.Drawing.Text
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Dim pfc As PrivateFontCollection = New PrivateFontCollection
    pfc.AddFontFile("digital-7 (italic).ttf")
    Label2.Font = New Font(pfc.Families(0), 30, FontStyle.Regular)
    Label2.ForeColor = Color.Black
    Dim pfc2 As PrivateFontCollection = New PrivateFontCollection
    pfc2.AddFontFile("digital-7.ttf")
    Label1.Font = New Font(pfc2.Families(0), 26, FontStyle.Regular)
    Label1.ForeColor = Color.Black
    BtnCase.Hide()
    BtnVer.Hide()
    BtnOff.Hide()
    BtnOn.Hide()
    BtnExp.Hide()
    BtnCube.Hide()
    BtnFact.Hide()
    BtnSqrt.Hide()
    Num7.Hide()
    Num8.Hide()
    Num9.Hide()
    BtnDel.Hide()
    BtnAC.Hide()
    Num4.Hide()
    Num5.Hide()
    Num6.Hide()
    BtnMult.Hide()
    BtnDiv.Hide()
    Num1.Hide()
    Num2.Hide()
    Num3.Hide()
    BtnAdd.Hide()
    BtnSub.Hide()
    Num0.Hide()
    BtnPi.Hide()
    BtnSqrd.Hide()
    BtnDot.Hide()
    BtnEql.Hide()
    BtnUp.Hide()
    BtnDown.Hide()
    BtnLeft.Hide()
    BtnRight.Hide()
End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Label2.Text = ""
    Timer1.Enabled = False
End Sub

Private Sub ButtonClickMethod(sender As Object, e As EventArgs) Handles BtnCase.Click, BtnVer.Click, BtnOff.Click, BtnOn.Click, BtnExp.Click, BtnCube.Click, BtnFact.Click, BtnSqrt.Click, Num7.Click, Num8.Click, Num9.Click, BtnDel.Click, BtnAC.Click, Num4.Click, Num5.Click, Num6.Click, BtnMult.Click, BtnDiv.Click, Num1.Click, Num2.Click, Num3.Click, BtnAdd.Click, BtnSub.Click, Num0.Click, BtnPi.Click, BtnSqrd.Click, BtnDot.Click, BtnEql.Click, BtnUp.Click, BtnDown.Click, BtnLeft.Click, BtnRight.Click
    Dim btn As Button = CType(sender, Button)

    If btn.Name = "BtnCase" Then
        Timer2.Enabled = True
        BtnCase.Hide()
        BtnVer.Hide()
        BtnOff.Hide()
        BtnOn.Hide()
        BtnExp.Hide()
        BtnCube.Hide()
        BtnFact.Hide()
        BtnSqrt.Hide()
        Num7.Hide()
        Num8.Hide()
        Num9.Hide()
        BtnDel.Hide()
        BtnAC.Hide()
        Num4.Hide()
        Num5.Hide()
        Num6.Hide()
        BtnMult.Hide()
        BtnDiv.Hide()
        Num1.Hide()
        Num2.Hide()
        Num3.Hide()
        BtnAdd.Hide()
        BtnSub.Hide()
        Num0.Hide()
        BtnPi.Hide()
        BtnSqrd.Hide()
        BtnDot.Hide()
        BtnEql.Hide()
        BtnUp.Hide()
        BtnDown.Hide()
        BtnLeft.Hide()
        BtnRight.Hide()
    End If

    If btn.Name = "BtnVer" Then
        Label1.Text = ""
        Label2.Text = "WE BARE BEARS"
    End If

    If btn.Name = "BtnOff" Then
        Label1.Text = ""
        Label2.Text = "CASEO"
        Timer1.Enabled = True
        BtnVer.Hide()
        BtnOff.Hide()
        BtnExp.Hide()
        BtnCube.Hide()
        BtnFact.Hide()
        BtnSqrt.Hide()
        Num7.Hide()
        Num8.Hide()
        Num9.Hide()
        BtnDel.Hide()
        BtnAC.Hide()
        Num4.Hide()
        Num5.Hide()
        Num6.Hide()
        BtnMult.Hide()
        BtnDiv.Hide()
        Num1.Hide()
        Num2.Hide()
        Num3.Hide()
        BtnAdd.Hide()
        BtnSub.Hide()
        Num0.Hide()
        BtnPi.Hide()
        BtnSqrd.Hide()
        BtnDot.Hide()
        BtnEql.Hide()
        BtnUp.Hide()
        BtnDown.Hide()
        BtnLeft.Hide()
        BtnRight.Hide()
    End If

    If btn.Name = "BtnOn" Then
        Label1.Text = ""
        Label2.Text = 0
        BtnVer.Show()
        BtnOff.Show()
        BtnExp.Show()
        BtnCube.Show()
        BtnFact.Show()
        BtnSqrt.Show()
        Num7.Show()
        Num8.Show()
        Num9.Show()
        BtnDel.Show()
        BtnAC.Show()
        Num4.Show()
        Num5.Show()
        Num6.Show()
        BtnMult.Show()
        BtnDiv.Show()
        Num1.Show()
        Num2.Show()
        Num3.Show()
        BtnAdd.Show()
        BtnSub.Show()
        Num0.Show()
        BtnPi.Show()
        BtnSqrd.Show()
        BtnDot.Show()
        BtnEql.Show()
        BtnUp.Show()
        BtnDown.Show()
        BtnLeft.Show()
        BtnRight.Show()
    End If

    If btn.Name = "BtnExp" Then
        Label1.Text = Label1.Text + "^"
    End If

    If btn.Name = "BtnCube" Then
        Label1.Text = Label1.Text + "^3"
    End If

    If btn.Name = "BtnFact" Then
        Label1.Text = Label1.Text + "!"
    End If

    If btn.Name = "BtnSqrt" Then
        Label1.Text = Label1.Text + "√"
    End If

    If btn.Name = "Num7" Then
        Label1.Text = Label1.Text + "7"
    End If

    If btn.Name = "Num8" Then
        Label1.Text = Label1.Text + "8"
    End If

    If btn.Name = "Num9" Then
        Label1.Text = Label1.Text + "9"
    End If

    If btn.Name = "BtnDel" Then
        ' Label1.Text = Label1.Text + "="
    End If

    If btn.Name = "BtnAC" Then
        ' Label1.Text = Label1.Text + "="
    End If

    If btn.Name = "Num4" Then
        Label1.Text = Label1.Text + "4"
    End If

    If btn.Name = "Num5" Then
        Label1.Text = Label1.Text + "5"
    End If

    If btn.Name = "Num6" Then
        Label1.Text = Label1.Text + "6"
    End If

    If btn.Name = "BtnMult" Then
        Label1.Text = Label1.Text + "×"
    End If

    If btn.Name = "BtnDiv" Then
        Label1.Text = Label1.Text + "÷"
    End If

    If btn.Name = "Num1" Then
        Label1.Text = Label1.Text + "1"
    End If

    If btn.Name = "Num2" Then
        Label1.Text = Label1.Text + "2"
    End If

    If btn.Name = "Num3" Then
        Label1.Text = Label1.Text + "3"
    End If

    If btn.Name = "BtnAdd" Then
        Label1.Text = Label1.Text + "+"
    End If

    If btn.Name = "BtnSub" Then
        Label1.Text = Label1.Text + "-"
    End If

    If btn.Name = "Num0" Then
        Label1.Text = Label1.Text + "0"
    End If

    If btn.Name = "BtnPi" Then
        Label1.Text = Label1.Text + "π"
    End If

    If btn.Name = "BtnSqrd" Then
        Label1.Text = Label1.Text + "^2"
    End If

    If btn.Name = "BtnDot" Then
        Label1.Text = Label1.Text + "."
    End If

    If btn.Name = "BtnEql" Then
        Label1.Text = Label1.Text + "="
    End If

    If btn.Name = "BtnUp" Then

    End If

    If btn.Name = "BtnDown" Then

    End If

    If btn.Name = "BtnLeft" Then

    End If

    If btn.Name = "BtnRight" Then

    End If

End Sub

Private Const INCREMENT As Integer = 9
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
    If PictureBox1.Location.Y >= 1050 Then
        Timer2.Enabled = False
        BtnCase.Show()
        BtnOn.Show()
    Else
        PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y + INCREMENT)
    End If
End Sub

Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
    Timer2.Enabled = True
End Sub

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
    If PictureBox1.Location.Y <= 0 Then
        Timer3.Enabled = False
    Else
        PictureBox1.Location = New Point(PictureBox1.Location.X, PictureBox1.Location.Y - INCREMENT)
    End If
End Sub
End Class

尽量不要重复代码。 Control.Hide() 的长列表中有两个是相同的,第三个仅在 2 个控件上有所不同(BtnCaseBtnOn)。我已将所有这些提取到一个名为 HideControls.

的方法中

Label1.Text = Label1.Text & "=" 的 shorthand 是 Label1.Text &= "=".

Select Case 比所有那些 If 语句更容易阅读和编写。

您的错误的实际解决方案在 Font 构造函数中。第二个参数需要 Single。当您为该参数输入文字 30 和 26 时,编译器会将其识别为 Int32 (Integer)。啊!错误消息提到的类型不匹配(Argument Exception)。您可以通过在数字后立即添加 F 来更正此问题。它将重新格式化为 30.026.0,编译器会将数字识别为 Single.

数字是数字,StringsText。您试图将 0 分配给 Text 属性。这不会用 Option Strict On 编译,而 Option Strict On 总是应该的。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim pfc As PrivateFontCollection = New PrivateFontCollection
    pfc.AddFontFile("digital-7 (italic).ttf")
    Label2.Font = New Font(pfc.Families(0), 30.0F, FontStyle.Regular)
    Label2.ForeColor = Color.Black
    Dim pfc2 As PrivateFontCollection = New PrivateFontCollection
    pfc2.AddFontFile("digital-7.ttf")
    Label1.Font = New Font(pfc2.Families(0), 26.0F, FontStyle.Regular)
    Label1.ForeColor = Color.Black
    BtnCase.Hide()
    BtnOn.Hide()
    HideControls()
End Sub

Private Sub ButtonClickMethod(sender As Object, e As EventArgs) Handles BtnCase.Click, BtnVer.Click, BtnOff.Click, BtnOn.Click, BtnExp.Click, BtnCube.Click, BtnFact.Click, BtnSqrt.Click, Num7.Click, Num8.Click, Num9.Click, BtnDel.Click, BtnAC.Click, Num4.Click, Num5.Click, Num6.Click, BtnMult.Click, BtnDiv.Click, Num1.Click, Num2.Click, Num3.Click, BtnAdd.Click, BtnSub.Click, Num0.Click, BtnPi.Click, BtnSqrd.Click, BtnDot.Click, BtnEql.Click, BtnUp.Click, BtnDown.Click, BtnLeft.Click, BtnRight.Click
    Dim btn As Button = CType(sender, Button)
    Select Case btn.Name
        Case "BtnCase"
            Timer2.Enabled = True
            BtnCase.Hide()
            BtnOn.Hide()
            HideControls()
        Case "BtnVer"
            Label1.Text = ""
            Label2.Text = "WE BARE BEARS"
        Case "BtnOff"
            Label1.Text = ""
                Label2.Text = "CASEO"
                Timer1.Enabled = True
                HideControls()
        Case "BtnOn"
            Label1.Text = ""
                Label2.Text = 0
                BtnVer.Show()
                BtnOff.Show()
                BtnExp.Show()
                BtnCube.Show()
                BtnFact.Show()
                BtnSqrt.Show()
                Num7.Show()
                Num8.Show()
                Num9.Show()
                BtnDel.Show()
                BtnAC.Show()
                Num4.Show()
                Num5.Show()
                Num6.Show()
                BtnMult.Show()
                BtnDiv.Show()
                Num1.Show()
                Num2.Show()
                Num3.Show()
                BtnAdd.Show()
                BtnSub.Show()
                Num0.Show()
                BtnPi.Show()
                BtnSqrd.Show()
                BtnDot.Show()
                BtnEql.Show()
                BtnUp.Show()
                BtnDown.Show()
                BtnLeft.Show()
                BtnRight.Show()
        Case "BtnExp"
            Label1.Text &= "^"
        Case "BtnCube"
            Label1.Text &= "^3"
        Case "BtnFact"
            Label1.Text &= "!"
        Case "BtnSqrt"
            Label1.Text &= "√"
        Case "Num7"
            Label1.Text &= "7"
        Case "Num8"
            Label1.Text &= "8"
        Case "Num9"
            Label1.Text &= "9"
        Case "Num4"
            Label1.Text &= "4"
        Case "Num5"
            Label1.Text &= "5"
        Case "Num6"
            Label1.Text &= "6"
        Case "BtnMult"
            Label1.Text &= "×"
        Case "BtnDiv"
            Label1.Text &= "÷"
        Case "Num1"
            Label1.Text &= "1"
        Case "Num2"
            Label1.Text &= "2"
        Case "Num3"
            Label1.Text &= "3"
        Case "BtnAdd"
            Label1.Text &= "+"
        Case "BtnSub"
            Label1.Text &= "-"
        Case "Num0"
            Label1.Text &= "0"
        Case "BtnPi"
            Label1.Text &= "π"
        Case "BtnSqrd"
            Label1.Text &= "^2"
        Case "BtnDot"
            Label1.Text &= "."
        Case "BtnEql"
            Label1.Text &= "="
    End Select
End Sub

Private Sub HideControls()
    BtnVer.Hide()
    BtnOff.Hide()
    BtnExp.Hide()
    BtnCube.Hide()
    BtnFact.Hide()
    BtnSqrt.Hide()
    Num7.Hide()
    Num8.Hide()
    Num9.Hide()
    BtnDel.Hide()
    BtnAC.Hide()
    Num4.Hide()
    Num5.Hide()
    Num6.Hide()
    BtnMult.Hide()
    BtnDiv.Hide()
    Num1.Hide()
    Num2.Hide()
    Num3.Hide()
    BtnAdd.Hide()
    BtnSub.Hide()
    Num0.Hide()
    BtnPi.Hide()
    BtnSqrd.Hide()
    BtnDot.Hide()
    BtnEql.Hide()
    BtnUp.Hide()
    BtnDown.Hide()
    BtnLeft.Hide()
    BtnRight.Hide()
End Sub

如果在设计时将每个按钮处理的 Tag 属性 设置为适当的字符串,则可以节省大量代码。将 "" 的按钮设置为 String.Empty。我们需要 .ToString 因为 Tag 属性 是 Object.

ShowControls 方法不会节省任何输入,但会使按钮点击代码更清晰。

Private Sub ButtonClickMethod(sender As Object, e As EventArgs) Handles BtnCase.Click, BtnVer.Click, BtnOff.Click, BtnOn.Click, BtnExp.Click, BtnCube.Click, BtnFact.Click, BtnSqrt.Click, Num7.Click, Num8.Click, Num9.Click, BtnDel.Click, BtnAC.Click, Num4.Click, Num5.Click, Num6.Click, BtnMult.Click, BtnDiv.Click, Num1.Click, Num2.Click, Num3.Click, BtnAdd.Click, BtnSub.Click, Num0.Click, BtnPi.Click, BtnSqrd.Click, BtnDot.Click, BtnEql.Click, BtnUp.Click, BtnDown.Click, BtnLeft.Click, BtnRight.Click
    Dim btn As Button = CType(sender, Button)
    Label1.Text = btn.Tag.ToString
    Select Case btn.Name
        Case "BtnCase"
            Timer2.Enabled = True
            BtnCase.Hide()
            BtnOn.Hide()
            HideControls()
        Case "BtnVer"
            Label2.Text = "WE BARE BEARS"
        Case "BtnOff"
            Label2.Text = "CASEO"
            Timer1.Enabled = True
            HideControls()
        Case "BtnOn"
            Label2.Text = "0"
            ShowControls()
    End Select
End Sub

Private Sub ShowControls()
    BtnVer.Show()
    BtnOff.Show()
    BtnExp.Show()
    BtnCube.Show()
    BtnFact.Show()
    BtnSqrt.Show()
    Num7.Show()
    Num8.Show()
    Num9.Show()
    BtnDel.Show()
    BtnAC.Show()
    Num4.Show()
    Num5.Show()
    Num6.Show()
    BtnMult.Show()
    BtnDiv.Show()
    Num1.Show()
    Num2.Show()
    Num3.Show()
    BtnAdd.Show()
    BtnSub.Show()
    Num0.Show()
    BtnPi.Show()
    BtnSqrd.Show()
    BtnDot.Show()
    BtnEql.Show()
    BtnUp.Show()
    BtnDown.Show()
    BtnLeft.Show()
    BtnRight.Show()
End Sub