谁能帮我一下?一切都运行得很顺利,但突然间,调试停止了然后它把我带到了这个->误差
更糟糕的是,我试图在一个新项目中重新进行整个设计和编码,认为这个问题只会出现在一个特定的项目中。浪费了大约两个小时。我把我的整个代码在这里,以防问题在于我的代码的某个地方,这将是很长,但请帮助我出来。我可能漏掉了什么。
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个不同的控制(BtnCase
和BtnOn
)。我将所有这些提取到一个称为HideControls
的方法中。
Label1.Text = Label1.Text & "="
的简写是Label1.Text &= "=".
Select Case
比所有那些If
语句更容易读和写。
错误的实际解决方案在Font
构造函数中。第二个参数期望是Single
。当您为这个参数输入文字30和26时,编译器将其识别为Int32
(Integer
)。啊!错误消息中提到的类型不匹配(参数异常)。您可以通过在数字后面立即添加F
来纠正这个问题。它将被重新格式化为30.0
和26.0
,并且编译器将该数字识别为Single
。
number为number,Strings
为Text
。您试图将0
分配给Text
属性。这将不会编译与选项严格,总是应该。
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