Counter for multiplication game - vb.net

Public Class Form1
' num1 and num2 now generate a number between 1 and 10
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim counter As Integer
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
' Generates a new question everytime the button is clicked
num1 = CInt(Int((10 * Rnd()) + 1))
num2 = CInt(Int((10 * Rnd()) + 1))
'Displays question in textbox2
TextBox2.Text = num1 & "*" & num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If TextBox1.Text = num1 * num2 Then
' If they get the answer right, they recieve postive feedback
Label2.Text = "Correct!!11"
Else
' Any other answer results in negative feedback
Label2.Text = "Incorrect, sorry about
This multiplication game is functioning as it should. I just need a way for the user of the game to be able to see how many questions they have gotten wrong and right. I need to implement a counter, but most of the guides online have proven useless. I decided to just post the code and see if someone could help me.

You could declare 2 new variables to store wrong and correct question values.
Dim wrongQuestions as Integer = 0
Dim correctQuestions as Integer = 0
If TextBox1.Text = num1 * num2 Then
' If they get the answer right, they recieve postive feedback
Label2.Text = "Correct!!11"
correctQuestions += 1
Else
' Any other answer results in negative feedback
Label2.Text = "Incorrect, sorry about"
wrongQuestions += 1
Then use
msgbox("Correct Questions: " & correctQuestions & " Incorrect Questions: " & wrongQuestions )

Using Random and with user input error checking
Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Me.AcceptButton = Button2
Button1.PerformClick()
End Sub
Dim prng As New Random
Dim answer As Integer
Dim incorrect As Integer = 0
Dim correct As Integer = 0
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
' Generates a new question everytime the button is clicked
Dim num1 As Integer = prng.Next(1, 11)
Dim num2 As Integer = prng.Next(1, 11)
TextBox2.Text = String.Format("{0} * {1}", num1, num2)
answer = num1 * num2
TextBox1.Text = ""
TextBox1.Select()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'check answer
Dim useranswer As Integer
If Integer.TryParse(TextBox1.Text, useranswer) Then
If useranswer = answer Then
correct += 1
Label2.Text = "Correct "
Else
incorrect += 1
Label2.Text = "Incorrect"
End If
Label2.Text &= String.Format(" Totals: correct = {0}, incorrect = {1}", _
correct, incorrect)
Button1.PerformClick()
Else
Label2.Text = "answer should be numeric"
End If
End Sub

Related

Changing Vector Directions

I'm trying to make a Pong knockoff in VB.net. However, I am having difficulty managing to get the ball object to change directions once it gets outside of the screen. This is what I have right now:
Option Explicit On
Option Infer Off
Option Strict On
Public Class Form1
'Declare variables here
Dim P1Score As Integer = 0
Dim P2Score As Integer = 0
Dim velocity As Integer = 1
Dim RandGen As New Random
Dim angle As Integer
'This function defines the X-value movement
Private Function angleCalcX(ByVal angle As Integer) As Integer
Dim xSpeed As Integer
xSpeed = Convert.ToInt16(ball.Location.X + (velocity * System.Math.Cos(angle)))
If ball.Bounds.IntersectsWith(Player1.Bounds) OrElse ball.Bounds.IntersectsWith(Player2.Bounds) Then
xSpeed = Convert.ToInt16(-(ball.Location.X + (velocity * System.Math.Cos(angle))))
End If
Return xSpeed
End Function
Private Function angleCalcY(ByRef angle As Integer) As Integer
Dim ySpeed As Integer
ySpeed = Convert.ToInt16(ball.Location.Y + (velocity * System.Math.Sin(angle)))
If (ball.Bounds.IntersectsWith(background.Bounds)) = False Then
ySpeed = Convert.ToInt16(-(ball.Location.Y + (velocity * System.Math.Sin(angle))))
End If
Return ySpeed
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Hello, and welcome to Pong! This is a 2-player game. Player 1 uses the W and S keys, and Player 2 uses the K and I keys. First to five goals wins. Press space to start!", "Start Screen.jpg", MessageBoxButtons.OK, MessageBoxIcon.Information)
angle = RandGen.Next(1, 360)
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
ball.Location = New Point(angleCalcX(angle), angleCalcY(angle))
If ball.Location.X > 1049 Then
P1Score += 1
velocity = 1
ElseIf ball.Location.X < 12 Then
P2Score += 1
velocity = 1
End If
End Sub
Public Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.S Then
Player1.Top += 25
ElseIf e.KeyCode = Keys.W Then
Player1.Top -= 25
ElseIf e.KeyCode = Keys.K Then
Player2.Top += 25
ElseIf e.KeyCode = Keys.I Then
Player2.Top -= 25
End If
End Sub
Private Sub quitButton_Click(sender As Object, e As EventArgs) Handles quitButton.Click
Me.Close()
End Sub
End Class
Can anyone help me out?
There's missing something like this in your Timer2_Tick
ElseIf ball.Location.Y < 12 OrElse ball.Location.Y > 600 Then
ySpeed = -ySpeed 'Make a bounce from top/bottom edge
But there's a lot to improve in the rest of code as well. I don't see the reason to use Int16 for speeds or hard-coding boundaries in code for example.

How to divide and get a whole number answer only in visual basic

Im making a math game atm, in the division section i need to make sure the question the computer asks is dividable and not give 2 random numbers which will result in a decimal. ie questions like 13/5 wont come up. How do i do that. Sorry if my indentation isnt correct im new to this interface.
Public Class Division
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(20, 40)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
Dim Score As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.KeyPreview = True
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Rnd1 As New Random
Dim Rnd2 As New Random
Dim Result0 = Rnd1.Next(20, 40)
Dim Result1 = Rnd1.Next(1, 10)
Dim Total = Result0 / Result1
If TextBox3.Text = Label3.Text Then
Score += 1
TextBox3.Text = ""
Else
MessageBox.Show("Incorrect")
TextBox3.Text = ""
End If
TextBox1.Text = Result0
TextBox2.Text = Result1
Label3.Text = Total
Label4.Text = Score
End Sub
Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
End Sub
Private Sub Form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Enter Then
e.SuppressKeyPress = True
Button1.PerformClick()
End If
End Sub
You can use cint
Example
Dim ans as integer = cint(ans1/ans2)
Try and check it.

VB.NET How to NOT subtract a larger number from a smaller number for textBoxResult using random generated numbers

Okay, so how do I do the part for display summary when i checked or unchecked?
Mine is not working for some reason. Am I missing out something? I have to give students the ability to control the display of the information.
Public Class MathPractice
Private Sub rbAddition_CheckedChanged(sender As Object, e As EventArgs) Handles rbAddition.CheckedChanged
'Change the label to plus
lblPlus.Text = " + "
End Sub
Private Sub rbSubtraction_CheckedChanged(sender As Object, e As EventArgs) Handles rbSubtraction.CheckedChanged
'Change the label to minus
lblPlus.Text = "-"
Dim intlbl1 As Integer = Val(lbl1.Text)
Dim intlbl2 As Integer = Val(lbl2.Text)
If rbSubtraction.Checked Then
Do While intlbl1 < intlbl2
If rbGrade1.Checked Then
intlbl1 = Random.Next(1, 10)
ElseIf rbGrade2.Checked Then
intlbl1 = Random.Next(10, 99)
End If
Loop
lbl1.Text = intlbl1.ToString
End If
End Sub
Private Sub rbGrade1_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade1.CheckedChanged
Dim random As New Random()
'Get random numbers between 1 and 10.
If rbGrade1.Checked Then
lbl1.Text = random.Next(1, 10).ToString
lbl2.Text = random.Next(1, 10).ToString
End If
End Sub
Private Sub rbGrade2_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade2.CheckedChanged
Dim random As New Random()
random.Next(10, 99)
'Get random numbers between 10 and 99.
If rbGrade2.Checked Then
lbl1.Text = random.Next(10, 99).ToString
lbl2.Text = random.Next(10, 99).ToString
End If
End Sub
Private Sub btnCheckAnswer_Click(sender As Object, e As EventArgs) Handles btnCheckAnswer.Click
'Check the answer whether it is right or wrong
'Display a message box showing right or wrong
Dim intlbl1 As Integer = Val(lbl1.Text)
Dim intlbl2 As Integer = Val(lbl2.Text)
Dim intResult As Integer = Val(txtBoxResult.Text)
Dim btnCheckAnswer As String
Select Case lblPlus.Text
Case "+"
If intResult = intlbl1 + intlbl2 Then
btnCheckAnswer = "Correct."
Else
btnCheckAnswer = "Sorry. Try Again."
End If
Case "-"
If intResult = intlbl1 - intlbl2 Then
btnCheckAnswer = "Correct."
Else
btnCheckAnswer = "Sorry. Try Again."
End If
End Select
End Sub
Private Sub chkBoxDisplaySummary_CheckedChanged(sender As Object, e As EventArgs) Handles chkBoxDisplaySummary.CheckedChanged
'Display or hide information
If chkBoxDisplaySummary.Checked Then
GroupBox2.Visible = True
Else
GroupBox2.Visible = False
End If
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
ok .. this should do it .. tho of course if the number in lbl2 is the biggest of the possible random numbers (10 or 99) lbl1 can never be bigger, so the code ive written could poddibly result in lbl1 and lbl2 both being 10 or 99. Anyhoe heres the code
ok ive recreated the app on my pc - there were a few errors in the original code that although it looked ok at a glance, threw things off - for example at the beginning when you assign a "+" to the lbl, you had a space either side of it, but in the checking answer logic there were no spaces. also cint on a string doesnt work when a visual studio opton called "option explicit" is turned on - having it on encourages better programming :) Also because of this "option explicit, you'll see ive added ".ToString in a few places as well
OK this is what ive got - i presume that your two sets of radio buttons are in two separate group boxes or the code would never have worked :)
have a look at the code below and compare it side by side with your original. Hope this helps. And dont forget the plus one if it does :-))
Private Sub rbAddition_CheckedChanged(sender As Object, e As EventArgs) Handles rbAddition.CheckedChanged
lblPlus.Text = "+"
End Sub
Private Sub rbSubtraction_CheckedChanged(sender As Object, e As EventArgs) Handles rbSubtraction.CheckedChanged
lblPlus.Text = "-"
Dim intlbl1 As Integer = Val(lbl1.Text)
Dim intlbl2 As Integer = Val(lbl2.Text)
If rbSubtraction.Checked Then
Do While intlbl1 < intlbl2
If rbGrade1.Checked Then
intlbl1 = random.Next(1, 10)
ElseIf rbGrade2.Checked Then
intlbl1 = random.Next(10, 99)
End If
Loop
lbl1.Text = intlbl1.ToString
End If
End Sub
Private Sub rbGrade1_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade1.CheckedChanged
Dim random As New Random()
'Get random numbers between 1 and 10.
If rbGrade1.Checked Then
lbl1.Text = random.Next(1, 10).ToString
lbl2.Text = random.Next(1, 10).ToString
End If
End Sub
Private Sub rbGrade2_CheckedChanged(sender As Object, e As EventArgs) Handles rbGrade2.CheckedChanged
Dim random As New Random()
random.Next(10, 99)
'Get random numbers between 10 and 99.
If rbGrade2.Checked Then
lbl1.Text = random.Next(10, 99).ToString
lbl2.Text = random.Next(10, 99).ToString
End If
End Sub
Private Sub btnCheckAnswer_Click(sender As Object, e As EventArgs) Handles btnCheckAnswer.Click
Dim intlbl1 As Integer = Val(lbl1.Text)
Dim intlbl2 As Integer = Val(lbl2.Text)
Dim intResult As Integer = Val(txtboxResult.Text)
Dim isTheAnswerCorrect As String
Select Case lblPlus.Text
Case "+"
If intResult = intlbl1 + intlbl2 Then
isTheAnswerCorrect = "Correct!"
Else
isTheAnswerCorrect = "Sorry. Try Again"
End If
Case "-"
If intResult = intlbl1 - intlbl2 Then
isTheAnswerCorrect = "Correct!"
Else
isTheAnswerCorrect = "Sorry. Try Again"
End If
End Select
MsgBox(isTheAnswerCorrect)
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
if cint(lbl1.Text)>=cint(lbl2.Text) then
intResult = cint(lbl1.Text)-cint(lbl2.Text)
else
intResult = cint(lbl2.Text)-cint(lbl1.Text)
end if
You can prompt the user to calculate the difference between two numbers(without sign(+/-)). then you can do calculation using Math.Abs()
intResult = Math.Abs(Val(lbl1.Text)-Val(lbl2.Text))

Make specific randomly generated codes with timer

I would like to make randomly generated codes when a timer is finished.
But I would like to use this code ABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789
on this format XXXX-XXXX-XXXX (replace X with a randomly generated number/letter)
And I would like to insert the code in this
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
TextBox1.Text = "replace textbox by code"
Timer1.Stop()
End If
End Sub
Here's a solution using LINQ.
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ProgressBar1.Increment(3)
If ProgressBar1.Value = 100 Then
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Dim random = New Random()
Dim num1 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num2 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim num3 = New String(Enumerable.Repeat(chars, 4).Select(Function(s) s(random.Next(s.Length))).ToArray())
Dim result = num1 & "-" & num2 & "-" & num3
TextBox1.Text = result
Timer1.Stop()
End If
End Sub
With this, you'd just change your TextBox1.Text = to point to the getNewRandomString() function.
Private Function getNewRandomString() As String
Dim sb As New StringBuilder
For i = 1 To 3
For j = 1 To 4
sb.Append(getNewRandomLetter())
Next
sb.Append("-")
Next
Return sb.ToString.Trim("-")
End Function
Private Function getNewRandomLetter() As Char
Dim chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
Return (chars(GetRandom(0, chars.Length - 1)))
End Function
Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
Static Generator As System.Random = New System.Random()
Return Generator.Next(Min, Max + 1)
End Function

not accessible in this context because it is 'Private' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm writing some code for a calculator and I keep getting this error. I have the math functions in another class but the variables from form1 are not accessible. Below is my code.
I've also tried changing my Dim variables to public but that also doesn't work.
Public Class Form1
'create a value to keep track of whether the calculation is complete so the calculator can revert to zero for new calculations
Dim state As Integer
'create values to store information on numbers and signs entered as well as the answer returned
Dim one As Double
Dim two As Double
Dim ans As Double
Dim sign As Char
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'when "Return" or "Enter" Key is selected button 15 is clicked (seems to only work when textbox selected????)
Me.AcceptButton = Button15
'change the title of the form
Me.Text = "Simple Calculator"
'label the buttons logically
Button1.Text = "1"
Button2.Text = "2"
Button3.Text = "3"
Button4.Text = "4"
Button5.Text = "5"
Button6.Text = "6"
Button7.Text = "7"
Button8.Text = "8"
Button9.Text = "9"
Button10.Text = "0"
Button11.Text = "+"
Button12.Text = "-"
Button13.Text = "X"
Button14.Text = "/"
Button15.Text = "Calc"
Button16.Text = "About Me"
Button17.Text = "Clear"
'allows form to revcieve key events
Me.KeyPreview = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'create action when button 1 is clicked
'if state is 1 then previous calculation was solved, then clear textbox to allow for new input
If state = 1 Then
TextBox1.Text = ""
'insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
'return state of calculator to zero to designate that calculation has NOT been solved
state = 0
Else
'else insert 1 into textbox
Dim Int1 As Integer = 1
TextBox1.Text = TextBox1.Text & Int1
End If
End Sub
' the above function for each numbered button
Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
state = 0
Else
Dim Int2 As Integer = 2
TextBox1.Text = TextBox1.Text & Int2
End If
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
state = 0
Else
Dim Int3 As Integer = 3
TextBox1.Text = TextBox1.Text & Int3
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
state = 0
Else
Dim Int4 As Integer = 4
TextBox1.Text = TextBox1.Text & Int4
End If
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
state = 0
Else
Dim Int5 As Integer = 5
TextBox1.Text = TextBox1.Text & Int5
End If
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
state = 0
Else
Dim Int6 As Integer = 6
TextBox1.Text = TextBox1.Text & Int6
End If
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
state = 0
Else
Dim Int7 As Integer = 7
TextBox1.Text = TextBox1.Text & Int7
End If
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
state = 0
Else
Dim Int8 As Integer = 8
TextBox1.Text = TextBox1.Text & Int8
End If
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
state = 0
Else
Dim Int9 As Integer = 9
TextBox1.Text = TextBox1.Text & Int9
End If
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
If state = 1 Then
TextBox1.Text = ""
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
state = 0
Else
Dim Int0 As Integer = 0
TextBox1.Text = TextBox1.Text & Int0
End If
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
'create an action for when addition button is clicked
Try
'when button is clicked dim sign and text in textbox
sign = "+"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
'repeat the above action for remainder of arithmic functions
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
Try
'when button is clicked dim sign and text in textbox
sign = "-"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
Try
sign = "*"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error" ' if error occurs return error in textbox
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click
Try
sign = "/"
one = TextBox1.Text
TextBox1.Text = ""
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click
'run functions based on sign stored during calculation
Try
two = TextBox1.Text
If sign = "+" Then
Calculator2.Math.add()
ElseIf sign = "-" Then
Calculator2.Math.minus()
ElseIf sign = "*" Then
Calculator2.Math.multiply()
ElseIf sign = "/" Then
Calculator2.Math.divide()
End If
'if user attempts to divide by zero return divide by zero error in textbox
If TextBox1.Text = "Infinity" Then
TextBox1.Text = "Divide by Zero Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End If
Catch ex As Exception
TextBox1.Text = "Error"
state = 1 'if error occurs return state to 1 to allow for new calculation
End Try
End Sub
Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click
'create message box that provides information about author
Dim msg = "Student Name: Emily Wong" & vbCrLf & "Student Number: 0692740" ' Define the message you want to see inside the message box.
Dim title = "About Me" ' Define a title for the message box.
Dim style = MsgBoxStyle.OkOnly ' make an ok button for the msg box
Dim response = MsgBox(msg, style, title)
End Sub
Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click
'create a clear button to clear textboxes when clicked and reset state of calculator
TextBox1.Text = ""
state = 0
End Sub
And this is my other class
Public Class Math
Inherits Form1
'create funtions for add,sub, multiply, and divide features
Sub add()
ans = one + two 'creates calculation of the entered numbers
TextBox1.Text = ans 'returns answer into the textbox
state = 1 'returns state to 1 to show that calculation has occured
End Sub
Sub minus()
ans = one - two
TextBox1.Text = ans
state = 1
End Sub
Sub multiply()
ans = one * two
TextBox1.Text = ans
state = 1
End Sub
Sub divide()
ans = one / two
TextBox1.Text = ans
state = 1
End Sub
End Class
state is a private variable in Form1. You can't access from code outside of Form1. Your Math class cannot reference state. Remove the calls to state from the Math class code. An outside class should not be changing the state of another object in anycase.
Try this instead:
Public Class accounting
Dim Operand1 As Double
Dim Operand2 As Double
Dim [Operator] As String
These are the button from 1 - 0
Private Sub Button6_Click_1(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button19.Click, Button12.Click, Button11.Click, Button10.Click
txtans.Text = txtans.Text & sender.text
End Sub
This button is for period/point
Private Sub Button18_Click(sender As Object, e As EventArgs) Handles Button18.Click
If InStr(txtans.Text, ".") > 0 Then
Exit Sub
Else
txtans.Text = txtans.Text & "."
End If
End Sub
This button is for clear or "C" in calculator
Private Sub Button20_Click(sender As Object, e As EventArgs) Handles Button20.Click
txtans.Text = ""
End Sub
These are for add,subtract,divide, and multiply
Private Sub Buttonadd_Click(sender As Object, e As EventArgs) Handles Button13.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "+"
End Sub
Private Sub Buttondivide_Click(sender As Object, e As EventArgs) Handles Button15.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "-"
End Sub
Private Sub Buttonsubtract_Click(sender As Object, e As EventArgs) Handles Button16.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "*"
End Sub
Private Sub Buttonmultiply_Click(sender As Object, e As EventArgs) Handles Button14.Click
Operand1 = Val(txtans.Text)
txtans.Text = ""
txtans.Focus()
[Operator] = "/"
End Sub
This button is for "=" sign
Private Sub Buttoneequal_Click(sender As Object, e As EventArgs) Handles Button17.Click
Dim Result As Double
Operand2 = Val(txtans.Text)
'If [Operator] = "+" Then
' Result = Operand1 + Operand2
'ElseIf [Operator] = "-" Then
' Result = Operand1 - Operand2
'ElseIf [Operator] = "/" Then
' Result = Operand1 / Operand2
'ElseIf [Operator] = "*" Then
' Result = Operand1 * Operand2
'End If
Select Case [Operator]
Case "+"
Result = Operand1 + Operand2
txtans.Text = Result.ToString("#,###.00")
Case "-"
Result = Operand1 - Operand2
txtans.Text = Result.ToString("#,###.00")
Case "/"
Result = Operand1 / Operand2
txtans.Text = Result.ToString("#,###.00")
Case "*"
Result = Operand1 * Operand2
txtans.Text = Result.ToString("#,###.00")
End Select
txtans.Text = Result.ToString("#,###.00")
End Sub
This button is for backspace effect.
Private Sub Buttondel_Click(sender As Object, e As EventArgs) Handles Button21.Click
If txtans.Text < " " Then
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1 + 1)
Else
txtans.Text = Mid(txtans.Text, 1, Len(txtans.Text) - 1)
End If
End Sub
Note that "txtans.text" is the textbox where the user inputs and where the output shows.