Can someone help me out here? Things were running smoothly but suddenly, the debugging stops then it leads me to this -> error
What's worse is I tried to do the entire design and code again in a new project thinking that that problem only appears in that one specific project. Wasted about two hours. I'm putting my entire code here in case the problem lies somewhere in my code, this will be long but please help me out. I may have missed something.
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
CodePudding user response:
Try not to duplicate code. Two of those long list of Control.Hide()
are identical, the third only differs by 2 controls (BtnCase
and BtnOn
). I have extracted all that to a single method called HideControls
.
A shorthand for Label1.Text = Label1.Text & "="
is Label1.Text &= "=".
Select Case
is easier to read and write rather than all those If
statements.
The actual solution to your error is in the Font
constructor. The second parameter is expecting a Single
. When you put the literal 30 and 26 for this parameter, the compiler recognizes this as Int32
(Integer
). Ah! The type mismatch (Argument Exception) that the error message mentions. You can correct this by adding an F
immediately after the number. It will reformat to 30.0
and 26.0
and the compiler will recognize the number as a Single
.
Numbers are numbers and Strings
are Text
. You tried to assign 0
to a Text
property. This will not compile with Option Strict On which is always should be.
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
You could save a lot of code if you set the Tag
property at design time of each button handled by to the appropriate string. Set the buttons with ""
to String.Empty
. We need the .ToString
because the Tag
property is an Object
.
The ShowControls
method doesn't save any typing but in makes the button click code much cleaner.
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