'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Calculator - vb.net

Im new to programming. This is my visual Basic code for an advanced calculator. I dont know how to fix the problem, I think I have to convert the textbox1 into interger since it is string but I don't know how to. Thanks for the help!
Option Explicit On
Public Class Form1
Dim op, FirstNumber As Integer
Dim K As Double
Dim I As Double
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "1"
Else
TextBox1.Text = TextBox1.Text & "1"
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "2"
Else
TextBox1.Text = TextBox1.Text & "2"
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "3"
Else
TextBox1.Text = TextBox1.Text & "3"
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "4"
Else
TextBox1.Text = TextBox1.Text & "4"
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "5"
Else
TextBox1.Text = TextBox1.Text & "5"
End If
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "6"
Else
TextBox1.Text = TextBox1.Text & "6"
End If
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "7"
Else
TextBox1.Text = TextBox1.Text & "7"
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "8"
Else
TextBox1.Text = TextBox1.Text & "8"
End If
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "9"
Else
TextBox1.Text = TextBox1.Text & "9"
End If
End Sub
Private Sub Button0_Click(sender As Object, e As EventArgs) Handles Button0.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "0"
Else
TextBox1.Text = TextBox1.Text & "0"
End If
End Sub
Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click
TextBox1.Text = "0"
End Sub
Private Sub DecimalButton_Click(sender As Object, e As EventArgs) Handles DecimalButton.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "."
Else
TextBox1.Text = TextBox1.Text & "."
End If
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
op = 1
FirstNumber = TextBox1.Text
TextBox1.Text = ""
End Sub
Private Sub SubtractButton_Click(sender As Object, e As EventArgs) Handles SubtractButton.Click
op = 2
FirstNumber = TextBox1.Text
TextBox1.Text = ""
End Sub
Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) Handles MultiplyButton.Click
op = 3
FirstNumber = TextBox1.Text
TextBox1.Text = ""
End Sub
Private Sub DivideButton_Click(sender As Object, e As EventArgs) Handles DivideButton.Click
op = 4
FirstNumber = TextBox1.Text
TextBox1.Text = ""
End Sub
Private Sub EqualButton_Click(sender As Object, e As EventArgs) Handles EqualButton.Click
If op = 1 Then
TextBox1.Text = Val(FirstNumber) + Val(TextBox1.Text)
ElseIf op = 2 Then
TextBox1.Text = Val(FirstNumber) - Val(TextBox1.Text)
ElseIf op = 3 Then
TextBox1.Text = Val(FirstNumber) * Val(TextBox1.Text)
ElseIf op = 4 Then
TextBox1.Text = Val(FirstNumber) / Val(TextBox1.Text)
ElseIf op = 5 Then
TextBox1.Text = Val(FirstNumber) * Val(FirstNumber)
ElseIf op = 6 Then
TextBox1.Text = Math.Sqrt(Val(FirstNumber))
ElseIf op = 7 Then
TextBox1.Text = Val(FirstNumber) ^ (1 / 3)
ElseIf op = 8 Then
TextBox1.Text = Val(FirstNumber) * Val(FirstNumber) * Val(FirstNumber)
ElseIf op = 9 Then
TextBox1.Text = Math.Tan(Val(FirstNumber))
ElseIf op = 10 Then
TextBox1.Text = Math.Cos(Val(FirstNumber))
ElseIf op = 11 Then
TextBox1.Text = Math.Sin(Val(FirstNumber))
End If
End Sub
Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
Private Sub SQButton_Click(sender As Object, e As EventArgs) Handles SQButton.Click
op = 5
FirstNumber = TextBox1.Text
End Sub
Private Sub SQRButton_Click(sender As Object, e As EventArgs) Handles SQRButton.Click
op = 6
FirstNumber = TextBox1.Text
End Sub
Private Sub Sqr3Button_Click(sender As Object, e As EventArgs) Handles Sqr3Button.Click
op = 7
FirstNumber = TextBox1.Text
End Sub
Private Sub CubeButton_Click(sender As Object, e As EventArgs) Handles CubeButton.Click
op = 8
FirstNumber = TextBox1.Text
End Sub
Private Sub TanButton_Click(sender As Object, e As EventArgs) Handles TanButton.Click
op = 9
FirstNumber = TextBox1.Text
End Sub
Private Sub CosButton_Click(sender As Object, e As EventArgs) Handles CosButton.Click
op = 10
FirstNumber = TextBox1.Text
End Sub
Private Sub SinButton_Click(sender As Object, e As EventArgs) Handles SinButton.Click
op = 11
FirstNumber = TextBox1.Text
End Sub
Private Sub PiButton_Click(sender As Object, e As EventArgs) Handles PiButton.Click
If TextBox1.Text = "0" Then
TextBox1.Text = "3.14159265359"
Else
TextBox1.Text = TextBox1.Text & "3.14159265359"
End If
End Sub
Private Sub BackSpaceButton_Click(sender As Object, e As EventArgs) Handles BackSpaceButton.Click
TextBox1.Focus()
SendKeys.Send("{BackSpace}")
End Sub
End Class

In some of the subroutines it is better to use DOUBLE types rather than INTEGER types, especially since you have a decimal point being used (as Chris mentioned)
For simplicity, I changed your integer type to SINGLE
if you get an overflow error then change it double instead of single
As for the changes, you can find them below.
Dim op as integer
Dim FirstNumber As double
Dim Result As Double
Private Sub EqualButton_Click(sender As Object, e As EventArgs) Handles EqualButton.Click
If op = 1 Then
Result = Cdbl(FirstNumber) + Cdbl(TextBox1.Text)
TextBox1.Text = Str(result)
ElseIf op = 2 Then
Result = Cdbl(FirstNumber) - Cdbl(TextBox1.Text)
TextBox1.Text = Str(result)
ElseIf op = 3 Then
Result = Cdbl(FirstNumber) * Cdbl(TextBox1.Text)
TextBox1.Text = Str(result)
ElseIf op = 4 Then
Result = Cdbl(FirstNumber) / Cdbl(TextBox1.Text)
TextBox1.Text = Str(result)
ElseIf op = 5 Then
Result = Cdbl(FirstNumber) * Cdbl(FirstNumber)
TextBox1.Text = Str(result)
ElseIf op = 6 Then
Result = Math.Sqrt(Cdbl(FirstNumber))
TextBox1.Text = Str(result)
ElseIf op = 7 Then
Result = Cdbl(FirstNumber) ^ (1 / 3)
TextBox1.Text = Str(result)
ElseIf op = 8 Then
Result = Cdbl(FirstNumber) * Cdbl(FirstNumber) * Cdbl(FirstNumber)
TextBox1.Text = Str(result)
ElseIf op = 9 Then
Result = Math.Tan(Cdbl(FirstNumber))
TextBox1.Text = Str(result)
ElseIf op = 10 Then
Result = Math.Cos(Cdbl(FirstNumber))
TextBox1.Text = Str(result)
ElseIf op = 11 Then
Result = Math.Sin(Cdbl(FirstNumber))
TextBox1.Text = Str(result)
End If
End Sub
Private Sub SQButton_Click(sender As Object, e As EventArgs) Handles SQButton.Click
op = 5
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub SQRButton_Click(sender As Object, e As EventArgs) Handles SQRButton.Click
op = 6
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub Sqr3Button_Click(sender As Object, e As EventArgs) Handles Sqr3Button.Click
op = 7
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub CubeButton_Click(sender As Object, e As EventArgs) Handles CubeButton.Click
op = 8
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub TanButton_Click(sender As Object, e As EventArgs) Handles TanButton.Click
op = 9
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub CosButton_Click(sender As Object, e As EventArgs) Handles CosButton.Click
op = 10
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub SinButton_Click(sender As Object, e As EventArgs) Handles SinButton.Click
op = 11
FirstNumber = Cdbl(TextBox1.Text)
End Sub
Private Sub AddButton_Click(sender As Object, e As EventArgs) Handles AddButton.Click
op = 1
FirstNumber = Cdbl(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub SubtractButton_Click(sender As Object, e As EventArgs) Handles SubtractButton.Click
op = 2
FirstNumber = Cdbl(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub MultiplyButton_Click(sender As Object, e As EventArgs) Handles MultiplyButton.Click
op = 3
FirstNumber = Cdbl(TextBox1.Text)
TextBox1.Text = ""
End Sub
Private Sub DivideButton_Click(sender As Object, e As EventArgs) Handles DivideButton.Click
op = 4
FirstNumber = Cdbl(TextBox1.Text)
TextBox1.Text = ""
End Sub

Related

How can I make a VB.NET MDAS Calculator?

I already have a code but I do not know how to add the MDAS function. Here is my code:
Public Class Form1
'variables-----------------------------------------------------------------------------
Dim inp As Double = 0
Dim oprtn As String
Dim expr As Boolean = False
'numbers and decimal input-----------------------------------------------------------------------------
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Number1.Click, Number9.Click, Number8.Click, Number7.Click, Number6.Click, Number5.Click, Number4.Click, Number3.Click, Number2.Click, Number0.Click, Period.Click
Dim num As Button = sender
If ((TextBox1.Text = "0") Or (expr)) Then
TextBox1.Clear()
TextBox1.Text = num.Text
expr = False
ElseIf (num.Text = ".") Then
If (Not TextBox1.Text.Contains(".")) Then
TextBox1.Text = TextBox1.Text + num.Text
End If
Else
TextBox1.Text = TextBox1.Text + num.Text
End If
End Sub
'operators button-----------------------------------------------------------------------------
Private Sub oprtrs(sender As Object, e As EventArgs) Handles PlusSign.Click, MinusSign.Click, MultiplySign.Click, DivideSign.Click
Dim operations As Button = sender
If (inp <> 0) Then
Equals.PerformClick()
expr = True
oprtn = operations.Text
Label1.Text = inp & " " & oprtn
Else
oprtn = operations.Text
inp = Double.Parse(TextBox1.Text)
expr = True
Label1.Text = inp & " " & oprtn
End If
End Sub
'equals button-----------------------------------------------------------------------------
Private Sub Equal_Click(sender As Object, e As EventArgs) Handles Equals.Click
Label1.Text = ""
Select Case oprtn
Case "+"
TextBox1.Text = (inp + Double.Parse(TextBox1.Text)).ToString()
Case "-"
TextBox1.Text = (inp - Double.Parse(TextBox1.Text)).ToString()
Case "*"
TextBox1.Text = (inp * Double.Parse(TextBox1.Text)).ToString()
Case "/"
TextBox1.Text = (inp / Double.Parse(TextBox1.Text)).ToString()
End Select
inp = Double.Parse(TextBox1.Text)
oprtn = ""
End Sub
'other buttons-----------------------------------------------------------------------------
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Backspace.Click
If TextBox1.Text < " " Then
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1 + 1)
Else
TextBox1.Text = Mid(TextBox1.Text, 1, Len(TextBox1.Text) - 1)
End If
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Clear.Click
TextBox1.Text = "0"
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles ClearEverything.Click
TextBox1.Text = "0"
Label1.Text = ""
End Sub
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Prcent.Click
Dim percent As Double
percent = Convert.ToDouble(TextBox1.Text) / Convert.ToDouble(100)
TextBox1.Text = System.Convert.ToString(percent)
End Sub
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles SqrRoot.Click
Dim squared As Double
squared = Math.Sqrt(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(squared)
Label1.Text = "Sqr(" & System.Convert.ToString(squared) & ")"
End Sub
Private Sub Button22_Click(sender As Object, e As EventArgs) Handles RaiseTwo.Click
Dim expo2 As Double
expo2 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(expo2)
End Sub
Private Sub Button23_Click(sender As Object, e As EventArgs) Handles RaiseThree.Click
Dim expo3 As Double
expo3 = Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text) * Convert.ToDouble(TextBox1.Text)
TextBox1.Text = System.Convert.ToString(expo3)
End Sub
Private Sub Button24_Click(sender As Object, e As EventArgs) Handles OneOver.Click
Dim ov1 As Double
ov1 = Convert.ToDouble(1.0 / Convert.ToDouble(TextBox1.Text))
TextBox1.Text = System.Convert.ToString(ov1)
End Sub
End Class
Thats pretty much all I have done, it works as intended, BUT it does not follow MDAS which prioritizes Multiplication first followed by Division then Addition then Subtraction. My design goes like this:
I started with the interface first then code, I haven't really designed it yet. This is just to give you an idea what is already coded or included in the code. Every button functions as should be.
This is what David is talking about.
There is very little code needed.
Public Class ExpressionCalculator
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles ButtonPlus.Click, ButtonMultiply.Click, ButtonMinus.Click, ButtonDot.Click, ButtonDivide.Click, Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button10.Click, Button1.Click
Dim tb = DirectCast(sender, Button)
TextBox1.Text &= tb.Text
End Sub
Private Sub ButtonCalculate_Click(sender As Object, e As EventArgs) Handles ButtonCalculate.Click
Dim table = New DataTable()
Dim result = table.Compute(TextBox1.Text, Nothing)
TextBox1.Text = result.ToString
End Sub
End Class

Multiplication application, visual basic. invalid cast exception

Public Class Form1
Dim randomNumberOne As New Random
Dim randomNumberTwo As New Random
Dim ChildGrade As Integer
Dim strChildGrade As Integer
Dim strcorrectCounter As Integer
Dim Correctcounter As Integer = 0
Dim WrongCounter As Integer = 0
Dim totalQuestions As Integer = 0
Dim percent As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ChildGrade = InputBox("Enter your age ", "age")
grade()
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberOne.Next(0, 12)
End Sub
Private Sub grade()
If ChildGrade <= 4 Then
Gradelbl.Text = "Preschool"
ElseIf ChildGrade = 5 Then
Gradelbl.Text = "Kindergarden"
ElseIf ChildGrade <= 10 Then
Gradelbl.Text = "Elementary"
ElseIf ChildGrade <= 13 Then
Gradelbl.Text = "Middle School"
ElseIf ChildGrade <= 18 Then
Gradelbl.Text = "High School"
ElseIf ChildGrade > 18 Then
Gradelbl.Text = "NA"
End If
End Sub
Private Sub RandomNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RandomNumberToolStripMenuItem.Click
NumbersGroupBox.Enabled = False
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End Sub
Private Sub SingleTableToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SingleTableToolStripMenuItem.Click
NumbersGroupBox.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirstNumberlbl.Text = "1"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FirstNumberlbl.Text = "2"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
FirstNumberlbl.Text = "3"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
FirstNumberlbl.Text = "4"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
FirstNumberlbl.Text = "5"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
FirstNumberlbl.Text = "6"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
FirstNumberlbl.Text = "7"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
FirstNumberlbl.Text = "8"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
FirstNumberlbl.Text = "9"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
FirstNumberlbl.Text = "10"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
FirstNumberlbl.Text = "11"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
FirstNumberlbl.Text = "12"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub CheckAnswer()
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
MsgBox("Please try again", MsgBoxStyle.Information, "Answer")
WrongCounter = WrongCounter + 1
If WrongCounter = 2 Then
MsgBox("The correct answer is " & FirstNumberlbl.Text * SecondNumberlbl.Text, MsgBoxStyle.Information, "Correct answer")
WrongCounter = 0
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
TextBox1.Text = ""
End If
End Sub
Private Sub rightAnswer()
If FirstNumberlbl.Text * SecondNumberlbl.Text = TextBox1.Text Then
Correctcounter = Correctcounter + 1
NumberCorrectlbl.Text = Correctcounter
TextBox1.Text = ""
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
totalQuestions = totalQuestions + 1
End Sub
Private Sub Enterbtn_Click(sender As Object, e As EventArgs) Handles Enterbtn.Click
CheckAnswer()
rightAnswer()
TotalCountlbl.Text = totalQuestions
PercentCorrectlbl.Text = ((Correctcounter * 100) / (totalQuestions * 100)) * 100
End Sub
End Class
I'm creating a multiplication application in VB that keeps count of how many questions the user gets right and how many they get wrong. When I run the program, it works fine in counting the amount right and totaling up the percentage, but when I test for when they get it wrong I get this error. enter image description here
if the image isn't visible, the error says,
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid
The program points to the problem being in the if statement in the rightAnswer() sub class but i'm not exactly sure what is wrong with it.
The problem is you are trying to do calculations with strings. Strings are text, not numbers.
This is not a full answer but enough to get you to understand the problem and change your code to fix it.
Don't do any calculations on the contents of text boxes and/or labels. Use the correct variables for such tasks. Use the controls for simply retrieving and displaying. This will save you a lot of heart-ache in the long run.
So for example, don't do:
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
You're checking a number in the TextBox to a calculation based on two labels, they are all text!
So think along the lines of doing calculations on a variable fit for purpose, an integer for example:
Example:
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Use these for the calculations, then, display the result as text.
If you are expecting a floating point number, a decimal, change the variable type as needed.
In your 'CheckAnswer' subroutine you should starting thinking along the lines of:
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
'We have something in the TextBox (you can check to allow only numbers to be entered in the TextBox.KeyDown/Press event)
If Not Integer.Parse(TextBox1.Text) = (FirstNumber * SecondNumber) Then
'Do what you need ...
End If
End If
There's a couple of other things too that could simplify your code.
You don't need 12 button events all doing the same thing.
In you Form1_Load Event put this:
Dim bTags() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10, Button11, Button12}
For t As Integer = 0 To bTags.Count - 1
bTags(t).Tag = t + 1
Next
Then delete all your 12 Button Click events and replace them with:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
Dim selectedButton As Button = CType(sender, Button)
FirstNumberlbl.Text = selectedButton.Tag.ToString
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Job done...
Also, you grade subroutine would be better of as a Select Case function:
In your Form1_Load replace: grade() with Gradelbl.Text = Grade() and your grade() subroutine replaced with:
Private Function Grade() As String
Select Case ChildGrade
Case < 5 : Return "Preschool"
Case = 5 : Return "Kindergarden"
Case < 11 : Return "Elementary"
Case < 14 : Return "Middle School"
Case < 19 : Return "High School"
Case Else : Return "N/A"
End Select
End Function
I hope some of the advice will be of help.
What you need to do is cast the values of the textbox. For example:
Convert.toInt32(FirstNumberlbl.Text)
or
Dim firstNumber as Integer
Integer.TryParse(FirstNumberlbl.Text, firstNumber)
Or assign a variable to the textbox and cast the variable instead of the textbox.

Handles clause requires a WithEvents variable defined in the containing type or one of its base type

So I'm very new to coding and especially Visual Basic, and after fidgeting around for a bit with open source code I only seem to have one recurring problem in my current Form1.vb file; Handles clause requires a WithEvents variable defined in the containing type or one of its base type. This is what is looks like:
Public Class Form1
Dim Firstnum As Decimal
Dim Secondnum As Decimal
Dim Operations As Integer
Dim Operator_selector As Boolean = False
Private lblHold As Object
Public Property TextBox1 As Object
WithEvents Btn1_click As Button
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "1"
Else
TextBox1.Text = "1"
End If
End Sub
Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "2"
Else
TextBox1.Text = "2"
End If
End Sub
Private Sub Btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "3"
Else
TextBox1.Text = "3"
End If
End Sub
Private Sub Btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "4"
Else
TextBox1.Text = "4"
End If
End Sub
Private Sub Btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "5"
Else
TextBox1.Text = "5"
End If
End Sub
Private Sub Btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "6"
Else
TextBox1.Text = "6"
End If
End Sub
Private Sub Btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "7"
Else
TextBox1.Text = "7"
End If
End Sub
Private Sub Btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "8"
Else
TextBox1.Text = "8"
End If
End Sub
Private Sub Btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "9"
Else
TextBox1.Text = "9"
End If
End Sub
Private Sub Btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
If TextBox1.Text <> "0" Then
TextBox1.Text += "0"
End If
End Sub
Private Sub BtnPoint_Click(sender As Object, e As EventArgs) Handles btnPoint.Click
If Not (TextBox1.Text.Contains(".")) Then
TextBox1.Text += "."
End If
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
TextBox1.Text = "0"
End Sub
Private Sub BtnPlus_Click(sender As Object, e As EventArgs) Handles btnPlus.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 1 'For addition
lblHold.Text = Firstnum.ToString + "+"
End Sub
Private Sub BtnMinus_Click(sender As Object, e As EventArgs) Handles btnMinus.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 2 'For subtraction
lblHold.Text = Firstnum.ToString + "-"
End Sub
Private Sub BtnMult_Click(sender As Object, e As EventArgs) Handles btnMult.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 3 'For multiplication
lblHold.Text = Firstnum.ToString + "*"
End Sub
Private Sub BtnDiv_Click(sender As Object, e As EventArgs) Handles btnDiv.Click
Firstnum = TextBox1.Text
TextBox1.Text = "0"
Operator_selector = True
Operations = 4 'For division
lblHold.Text = Firstnum.ToString + "+"
End Sub
Private Sub BtnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
If Operator_selector = True Then
Secondnum = TextBox1.Text
If Operations = 1 Then
TextBox1.Text = Firstnum + Secondnum
ElseIf Operations = 2 Then
TextBox1.Text = Firstnum - Secondnum
ElseIf Operations = 3 Then
TextBox1.Text = Firstnum * Secondnum
Else
If Secondnum = 0 Then
TextBox1.Text = "Error!"
Else
TextBox1.Text = Firstnum / Secondnum
End If
Operator_selector = False
End If
lblHold.Text = ""
End If
End Sub
End Class
Now I'm wondering what I have to do to get it right and make this error disappear. I tried using something around the lines of "WithEvents Btn1_click As Button" but it doesn't do anything. It also says "Btn1_click is already declared as 'Private WithEvents Btn1_click As Button' in this class." I'm truly a noob, so please don't throw hardcore coding terms at me but just the simple stuff :P
Many thanks in advance!
The Handles clause you see on those methods indicates that that method will be executed when the specified event of the object currently assigned to the specified variable is raised. For example:
Private Sub Btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
means that the Btn2_Click method will be executed when the object assigned to the btn2 variable raises its Click event. The error message flags this line:
Private Sub Btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
and it's telling you that you have no btn1 variable declared WithEvents.
When you add a control to your form in the designer, it automatically adds a variable with the specified name and declares it WithEvents. You can see that variable if you open the designer code file, which you can access via the Solution Explorer if you click the Show All Files button first. You can declare your own variables WithEvents but this:
WithEvents Btn1_click As Button
is not using the correct name for a start and it is also useless because it doesn;t have anything assigned to it. You'd need to create a Button object, assign it to the variable and add it to the form, which you haven't done.
The solution would be to simply add a Button to the form with that name.

simple vb.net calculator for adding multiple numbers

What I would like to do is how normal people add numbers i.e. they will push the number button and click on the plus button in this order
2 + 5 + 6 + 8 (calculator keeps adding the numbers up and shows the addition results). in vb.net
I will help you with this code below:
Here is how your form should look like:
Now: add 5 buttons: "2" {Num2BUT}, "5" {Num5BUT}, "6" {Num6BUT}, "8" {Num8BUT}, "+" {PlusBUT}
and 2 TexBoxs: {ANTextBox}, {ResultTextBox}.
And here is the code:
Public Class Form1
Dim X1, X2 As Integer
Private Sub Num2BUT_Click(sender As Object, e As EventArgs) Handles Num2BUT.Click
ANTextBox.Text = ANTextBox.Text + "2 "
If X1 = 0 Then
X1 = 2
ElseIf X2 = 0 Then
X2 = 2
Else
X2 = 2
End If
End Sub
Private Sub Num5BUT_Click(sender As Object, e As EventArgs) Handles Num5BUT.Click
ANTextBox.Text = ANTextBox.Text + "5 "
If X1 = 0 Then
X1 = 5
ElseIf X2 = 0 Then
X2 = 5
Else
X2 = 5
End If
End Sub
Private Sub Num6BUT_Click(sender As Object, e As EventArgs) Handles Num6BUT.Click
ANTextBox.Text = ANTextBox.Text + "6 "
If X1 = 0 Then
X1 = 6
ElseIf X2 = 0 Then
X2 = 6
Else
X2 = 6
End If
End Sub
Private Sub Num8BUT_Click(sender As Object, e As EventArgs) Handles Num8BUT.Click
ANTextBox.Text = ANTextBox.Text + "8 "
If X1 = 0 Then
X1 = 8
ElseIf X2 = 0 Then
X2 = 8
Else
X2 = 8
End If
End Sub
Private Sub PlusBUT_Click(sender As Object, e As EventArgs) Handles PlusBUT.Click
If Not ANTextBox.Text = "" Then
If ResultTextBox.Text = "" Then
ANTextBox.Text = ANTextBox.Text + "+ "
If Not X1 = 0 And Not X2 = 0 Then
ResultTextBox.Text = X1 + X2
End If
Else
ANTextBox.Text = ANTextBox.Text + "+ "
ResultTextBox.Text = ResultTextBox.Text + X2
End If
End If
End Sub
End Class
Note:
This code just for 4 numbers, so you have just to add more buttons and copy the code of one of these buttons and change the number (X1 = "1", X1 = "9"....ect).
The way I understand it is you want to have two textboxes. One of them will show the result and the second one is used to type the numbers then the plus sign.
Here is a very simple solution:
Let's assume that TextBox2 is the textbox that will handle the number entries:
Dim NumberToHold As Double = 0
Private Sub TextBox2_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox2.TextChanged
If TextBox2.Text.Contains("+") Then
TextBox2.Text = TextBox2.Text.Replace("+", "")
TextBox2.Text = TextBox2.Text.Replace(" ", "")
NumberToHold = Val(TextBox2.Text) + NumberToHold
TextBox1.Text = NumberToHold
TextBox2.Clear()
End If
End Sub
Source: Simple addition calculator- VB.NET
OK, here is how your form should look like:
Now, like the first one; add 5 buttons: "2" {Num2BUT}, "5" {Num5BUT}, "6" {Num6BUT}, "8" {Num8BUT}, "+" {PlusBUT}
but now add one TexBox for result: {ANTResultTextBox}, and a hidden textBox {HiddenTextBox}
And here is the code:
Public Class Form1
Dim X1, X2 As Integer
Private Sub Num2BUT_Click(sender As Object, e As EventArgs) Handles Num2BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 2
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "2"
X1 = 2
End If
End Sub
Private Sub Num5BUT_Click(sender As Object, e As EventArgs) Handles Num5BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 5
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "5"
X1 = 5
End If
End Sub
Private Sub Num6BUT_Click(sender As Object, e As EventArgs) Handles Num6BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 6
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "6"
X1 = 6
End If
End Sub
Private Sub Num8BUT_Click(sender As Object, e As EventArgs) Handles Num8BUT.Click
If HiddenTextBox.Text = "Plus" Then
X2 = 8
ANTextBox.Text = ANTextBox.Text + X2
HiddenTextBox.Text = ""
Else
ANTextBox.Text = ANTextBox.Text & "8"
X1 = 8
End If
End Sub
Private Sub PlusBUT_Click(sender As Object, e As EventArgs) Handles PlusBUT.Click
ANTextBox.Text = ANTextBox.Text + " + "
HiddenTextBox.Text = "Plus"
End Sub
End Class
Here is the code and the form is basically designed as a textbox and a button for add, subtract, multiply and divide. The code enables addition, subtraction, multiplication and divisions of the numbers with one digit but should do in your case.
Public Class FrmCalculator
Dim PreviousNumber, NextNumber, DecPlace, Dec As Decimal
Dim CurrentNumber, State As String
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
CurrentNumber = 1
txtNumber.Text = "1"
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
CurrentNumber = 2
txtNumber.Text = "2"
End Sub
Private Sub btn3_Click(sender As Object, e As EventArgs) Handles btn3.Click
CurrentNumber = 3
txtNumber.Text = "3"
End Sub
Private Sub btn4_Click(sender As Object, e As EventArgs) Handles btn4.Click
CurrentNumber = 4
txtNumber.Text = "4"
End Sub
Private Sub btn5_Click(sender As Object, e As EventArgs) Handles btn5.Click
CurrentNumber = 5
txtNumber.Text = "5"
End Sub
Private Sub btn6_Click(sender As Object, e As EventArgs) Handles btn6.Click
CurrentNumber = 6
txtNumber.Text = "6"
End Sub
Private Sub btn7_Click(sender As Object, e As EventArgs) Handles btn7.Click
CurrentNumber = 7
txtNumber.Text = "7"
End Sub
Private Sub btn8_Click(sender As Object, e As EventArgs) Handles btn8.Click
CurrentNumber = 8
txtNumber.Text = "8"
End Sub
Private Sub btn9_Click(sender As Object, e As EventArgs) Handles btn9.Click
CurrentNumber = 9
txtNumber.Text = "9"
End Sub
Private Sub btnEqual_Click(sender As Object, e As EventArgs) Handles btnEqual.Click
If State = "M" Then
NextNumber = PreviousNumber * CurrentNumber
ElseIf State = "A" Then
NextNumber = PreviousNumber + CurrentNumber
ElseIf State = "S" Then
NextNumber = PreviousNumber - CurrentNumber
ElseIf State = "D" Then
NextNumber = PreviousNumber / CurrentNumber
txtNumber.Text = NextNumber
End Sub
Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles btnSubtract.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "S"
End Sub
Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles btnDivide.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "D"
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "A"
End Sub
Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles btnMultiply.Click
PreviousNumber = CurrentNumber
txtNumber.Clear()
State = "M"
'NextNumber = PreviousNumber * CurrentNumber
End Sub

VB.Net - Noncooperative decimal in string

I am writing a calculator WinForm in VB. I have a label used to display the output "Result". My problem comes when trying to add a "." to my label string. Example: I will type 355.5 and until the 5 is pressed after it, my string is showing up as .355 After the last 5 is pressed, it jumps into the correct location. I have exhausted my debugging skill and now am just going crazy. Anyone encounter this before?
Here's my entire code so far (ignore unfinished functions)
Public Class MyCalc
Private bDecFlag As Boolean
Private sMathOp As String
Private Sub ModeSel_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ModeSel.SelectedIndexChanged
If ModeSel.SelectedIndex = 3 Then
Me.Width = 360
ElseIf ModeSel.SelectedIndex > 2 Then
Me.Width = 590
Else
Me.Width = 250
End If
If ModeSel.SelectedIndex = 0 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
End If
If ModeSel.SelectedIndex = 1 Then
For iCount As Integer = 2 To 9
Me.Controls("Digit" & iCount).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 2 Then
For iCount As Integer = 0 To 7
Me.Controls("Digit" & iCount).Enabled = True
Next
Digit8.Enabled = False
Digit9.Enabled = False
End If
If ModeSel.SelectedIndex = 3 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("F")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
For iCount As Integer = Asc("G") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = False
Next
End If
If ModeSel.SelectedIndex = 4 Then
For iCount As Integer = 0 To 9
Me.Controls("Digit" & iCount).Enabled = True
Next
For iCount As Integer = Asc("A") To Asc("Z")
Me.Controls("Alpha" & Chr(iCount)).Enabled = True
Next
AlphaA.Enabled = False
AlphaE.Enabled = False
AlphaI.Enabled = False
AlphaO.Enabled = False
AlphaU.Enabled = False
End If
End Sub
Private Sub Digit0_Click(sender As System.Object, e As System.EventArgs) Handles Digit0.Click
UpdateResult(0)
End Sub
Private Sub Digit1_Click(sender As System.Object, e As System.EventArgs) Handles Digit1.Click
UpdateResult(1)
End Sub
Private Sub Digit2_Click(sender As System.Object, e As System.EventArgs) Handles Digit2.Click
UpdateResult(2)
End Sub
Private Sub Digit3_Click(sender As System.Object, e As System.EventArgs) Handles Digit3.Click
UpdateResult(3)
End Sub
Private Sub Digit4_Click(sender As System.Object, e As System.EventArgs) Handles Digit4.Click
UpdateResult(4)
End Sub
Private Sub Digit5_Click(sender As System.Object, e As System.EventArgs) Handles Digit5.Click
UpdateResult(5)
End Sub
Private Sub Digit6_Click(sender As System.Object, e As System.EventArgs) Handles Digit6.Click
UpdateResult(6)
End Sub
Private Sub Digit7_Click(sender As System.Object, e As System.EventArgs) Handles Digit7.Click
UpdateResult(7)
End Sub
Private Sub Digit8_Click(sender As System.Object, e As System.EventArgs) Handles Digit8.Click
UpdateResult(8)
End Sub
Private Sub Digit9_Click(sender As System.Object, e As System.EventArgs) Handles Digit9.Click
UpdateResult(9)
End Sub
Private Sub DecBut_Click(sender As System.Object, e As System.EventArgs) Handles DecBut.Click
If bDecFlag = False Then
If Result.Text = "0" Then
Result.Text = "0."
bDecFlag = True
Else
Result.Text = Result.Text + "."
bDecFlag = True
End If
End If
End Sub
Private Sub ClrBut_Click(sender As System.Object, e As System.EventArgs) Handles ClrBut.Click
Result.Text = 0
bDecFlag = False
End Sub
Private Sub DelBut_Click(sender As System.Object, e As System.EventArgs) Handles DelBut.Click
If Result.Text = "0" Then
ElseIf Result.Text.Substring(Result.Text.Length - 1) = "." Then
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
bDecFlag = False
Else
Result.Text = Result.Text.Substring(0, Result.Text.Length - 1)
If Result.Text = "" Then
Result.Text = "0"
End If
End If
End Sub
Private Sub MyCalc_KeyDown(sender As Object, e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
Case Keys.NumPad0
Digit0_Click(Digit0, New EventArgs)
Case Keys.NumPad1
Digit1_Click(Digit1, New EventArgs)
Case Keys.NumPad2
Digit2_Click(Digit2, New EventArgs)
Case Keys.NumPad3
Digit3_Click(Digit3, New EventArgs)
Case Keys.NumPad4
Digit4_Click(Digit4, New EventArgs)
Case Keys.NumPad5
Digit5_Click(Digit5, New EventArgs)
Case Keys.NumPad6
Digit6_Click(Digit6, New EventArgs)
Case Keys.NumPad7
Digit7_Click(Digit7, New EventArgs)
Case Keys.NumPad8
Digit8_Click(Digit8, New EventArgs)
Case Keys.NumPad9
Digit9_Click(Digit9, New EventArgs)
Case Keys.Back
DelBut_Click(DelBut, New EventArgs)
Case Keys.Decimal
DecBut_Click(DecBut, New EventArgs)
End Select
End Sub
Private Sub MyCalc_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
bDecFlag = False
End Sub
Public Sub UpdateResult(ByVal sNum As String)
If Result.Text = "0" Then
Result.Text = sNum
Else
Result.Text &= sNum
End If
End Sub
End Class
So after the long debugging, I found out that you are using the Label/Textbox RightToLeft property set to yes. Nothing is wrong with it really, but it can't handle special characters including decimal points, commas, etc. properly. The workaround for this is to reverse what we have expected - to reverse the string concatenation.
Result.Text = "." & Result.Text
Not seeing entire code I'm not exactly sure but I think there might be something wrong with string concatenation.
Try changing into this:
Result.Text = Result.Text & "."