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

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.

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.

'Cannot perform '=' operation on System.Int32 and System.String.'

I am using textbox as search for number, when I put number it searched but when I erased it from box I got error.
my code is:
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
SpringDataBindingSource.Filter = "SerialNumber = '" + TextBox2.Text + "'"
End Sub
the error is:
Cannot perform '=' operation on System.Int32 and System.String.
The problem is that empty string validation failed.
For empty string you should remove the filter.
It should be something like this:
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
if Trim(TextBox2.Text) = "" Then
SpringDataBindingSource.Filter = ""
Else
SpringDataBindingSource.Filter = "SerialNumber = '" + TextBox2.Text + "'"
End If
End Sub
EDIT
You can also check each inserted data with KeyUp event:
Private Sub TextBox2_KeyUp(sender As Object, e As KeyEventArgs) Handles ComboBox1.KeyUp
If NumericMode Then ' NumericMode is boolean and should be turnd On/Off elswhere
If Char.IsDigit(ChrW(e.KeyValue)) Then
'OK
Else
'ERROR
End If
End If
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
'This would work, but would consider decimals also a number
If IsNumeric(TextBox2.Text) Then
SpringDataBindingSource.Filter = String.Format("SerialNumber = '{0}'", TextBox2.Text)
Return
End If
SpringDataBindingSource.Filter = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
'This would work if you want to guarantee an Int32
Dim txtInt As Integer
If Int32.TryParse(TextBox2.Text, TxtInt) Then
SpringDataBindingSource.Filter = String.Format("SerialNumber = '{0}'", txtInt)
Else
SpringDataBindingSource.Filter = ""
End If
End Sub

'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll Calculator

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

How to store last button clicked and add to listbox

So I'm doing this calculator program and need the numbers, the operator used and the "=" sign to show up in the listbox so "1 + 1 = 2" should show up. I have the calculator working and can move items to the listbox, but can't figure out how to remember the button that was clicked to get the outcome and move it over also.
Option Explicit On
Option Strict On
Option Infer Off
Public Class Form1
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & NumTwo.Text & Result.Text)
End Sub
End Class
As far as you just need to store one character, you can rely on the Tag property of ListBox1 (basically, a black box where you can store anything you want). Sample code:
Private Sub ButtonAdd_Click(sender As Object, e As EventArgs) Handles ButtonAdd.Click
Result.Text = CStr(Val(NumOne.Text) + Val(NumTwo.Text))
ListBox1.Tag = "+"
End Sub
Private Sub ButtonSub_Click(sender As Object, e As EventArgs) Handles ButtonSub.Click
Result.Text = CStr(Val(NumOne.Text) - Val(NumTwo.Text))
ListBox1.Tag = "-"
End Sub
Private Sub ButtonMul_Click(sender As Object, e As EventArgs) Handles ButtonMul.Click
Result.Text = CStr(Val(NumOne.Text) * Val(NumTwo.Text))
ListBox1.Tag = "x"
End Sub
Private Sub ButtonDiv_Click(sender As Object, e As EventArgs) Handles ButtonDiv.Click
Result.Text = CStr(Val(NumOne.Text) / Val(NumTwo.Text))
'outputs a message box telling the user to correct the division by 0, also displays a blank result box instead of NaN
If CDbl(NumTwo.Text) = Val(0) Then
Result.Text = ""
MessageBox.Show("You cannot divide by 0, please input another number")
End If
ListBox1.Tag = "/"
End Sub
Private Sub ButtonExit_Click(sender As Object, e As EventArgs) Handles ButtonExit.Click
Me.Close()
End Sub
Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
ListBox1.Items.Add(NumOne.Text & ListBox1.Tag.ToString() & NumTwo.Text & "=" & Result.Text)
End Sub