Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
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
' It is meant to display the question on textbox2.text, not the answer
TextBox2.Text = num1 * num2
End Sub
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text = num1 * num2 Then
Label2.Text = "Correct!!11"
Else
Label2.Text = "Incorrect, sorry about that"
End If
End Sub
End Class
I want textbox2 to display a question like " 5 * 5" but instead it just displays "25"
You are assigning the value to the textbox as a multiply of num1, and num2. You need to concatenate the string values like this
TextBox2.Text = num1 & " * " & num2
Related
So i had to make a program where you can buy stuff when clicking on the buttons, when the amount totals more than 1k you can roll for a discount. This all goes well until I have to use a label to indicate the total amount you need to pay minus the discount. This label keeps staying 0, no matter what. Could anyone help?
Form1 for the "shop":
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Double
Public Sub Actie()
If Val(TextBox3.Text) > 1000 And i = 1 Then
If MsgBox("Wil je strijden voor korting? Zo niet zal deze pop-up niet meer komen dus maak je keuze!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label6.Text = Val(Label6.Text) + 1
TextBox1.Text = Val(TextBox1.Text) + 250
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = Val(Label7.Text) + 1
TextBox2.Text = Val(TextBox2.Text) + 300
TextBox3.Text = Val(TextBox1.Text) + Val(TextBox2.Text)
Actie()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Form2 for the gambling:
Public Class Form2
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
korting = Label1.Text
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = Int(Rnd() * 10)
End Sub
End Class
Module to have a value as a public one:
Module Module1
Public korting As Integer = 0
End Module
You basically should pay attention to this code of line since I think this is where all goes wrong:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = Val(totaalbedrag)
End Sub
I am not at all sure of the timer event in Form1. Every time it ticks the discount is reapplied. If the program runs long enouth totaalbedrag will be very small.
I used .ToString("C") or "c" to display the result as currency. Some of displays are probably not currency. Change to the appropriate format string. https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings
For Random numbers please switch to the .net Random class.
Public Class Form1
Dim i As Integer = 1
Dim totaalbedrag As Decimal
Public Sub Actie()
Dim tb3Dec As Decimal
If Decimal.TryParse(TextBox3.Text, tb3Dec) AndAlso tb3Dec > 1000 AndAlso i = 1 Then
If MsgBox("Do you want to compete for a discount? If not, this pop-up will not come again so make your choice!", vbYesNo) = vbYes Then
Me.Hide()
Form2.Show()
Else
i = 0
End If
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
TextBox4.Text = korting & " procent"
totaalbedrag = totaalbedrag - totaalbedrag * korting / 100
TextBox5.Text = totaalbedrag.ToString("C")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1.")
Exit Sub
End If
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Label6.Text = (CDec(Label6.Text) + 1).ToString
TextBox3.Text = (tb1Dec + tb2Dec).ToString("c")
TextBox1.Text = (tb1Dec + 250).ToString("C")
Actie()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Label7.Text = (CDec(Label7.Text) + 1).ToString("c")
Dim tb2Dec As Decimal
If Not Decimal.TryParse(TextBox2.Text, tb2Dec) Then
MessageBox.Show("Please enter a valid number in TextBox2")
Exit Sub
End If
Dim tb1Dec As Decimal
If Not Decimal.TryParse(TextBox1.Text, tb1Dec) Then
MessageBox.Show("Please enter a valid number in TextBox1")
Exit Sub
End If
TextBox2.Text = (tb2Dec + 300).ToString("C")
TextBox3.Text = (tb1Dec + tb2Dec).ToString("C")
Actie()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
MsgBox("Gefeliciteerd met de aankoop van totaal " & totaalbedrag & " euro!!", vbCritical)
End
End Sub
End Class
Public Class Form2
Private rand As New Random
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
korting = CInt(Label1.Text)
MsgBox("Gefeliciteerd, je hebt " & korting & " procent korting gekregen!", vbCritical)
Me.Hide()
Form1.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label1.Text = rand.Next(0, 101).ToString 'returns an integer 0 to 100
End Sub
End Class
This fixes the Option Strict violations but I have no idea it this works for you. Let us know which label is showing 0.
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
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
I created a VB.net calculator application.It works fine but say I want to add 5+7.Then as my application works I can press 5 from the keyboard as I gave the text of button with digit 5 as &5 but then if I press + from the keyboard it doesn't work.I have to press the + button in the calculator.
I think that's because my add button is designed to handle click event as btn_add_Click.Is there a way I can make this application to work so that without pressing the buttons in the calculator I can press the keys in keyboard and do the computation.Here's my code:
Imports System.Math
Public Class Form1
Private isFirstExist As Boolean
Private inputOperator As String
Private secondNum As Decimal
Private firstNum As Decimal
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtCalc.TextChanged
End Sub
Private Sub btn_zero_Click(sender As System.Object, e As System.EventArgs) Handles btn_zero.Click
removeFrontZero(0)
End Sub
Private Sub btn_one_Click(sender As System.Object, e As System.EventArgs) Handles btn_one.Click
removeFrontZero(1)
End Sub
Private Sub btn_two_Click(sender As System.Object, e As System.EventArgs) Handles btn_two.Click
removeFrontZero(2)
End Sub
Private Sub btn_clear_Click(sender As System.Object, e As System.EventArgs) Handles btn_clear.Click
txtCalc.Clear()
txtCalc.Text = "0"
End Sub
'Remove zero which is at the start
Public Sub removeFrontZero(ByVal digit As Integer)
If txtCalc.Text = "0" Then
txtCalc.Text = CStr(digit)
Else
txtCalc.Text &= digit
End If
End Sub
Private Sub btn_add_Click(sender As System.Object, e As System.EventArgs) Handles btn_add.Click
inputOperator = "+"
isFirst()
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
isFirstExist = False
End Sub
Private Sub btn_equal_Click(sender As System.Object, e As System.EventArgs) Handles btn_equal.Click
If isFirstExist Then
secondNum = CType(txtCalc.Text, Decimal)
End If
'Calculating the result
Dim result As Decimal = calculate(firstNum, secondNum, inputOperator)
txtCalc.Text = result.ToString()
isFirstExist = False
End Sub
Private Function calculate(ByVal num1 As Decimal, ByVal num2 As Decimal, ByVal inputOp As String) As Decimal
Dim output As Decimal
firstNum = num1
secondNum = num2
Select Case inputOp
Case "+"
output = num1 + num2
Case "-"
output = num1 - num2
Case "/"
Dim value As Decimal
Try
isFirst()
value = (num1 / num2)
Catch ex As DivideByZeroException
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK)
End Try
output = value
Case "*"
output = num1 * num2
Case "Mod"
output = (num1 Mod num2)
Case "^"
output = CDec(Math.Pow(num1, num2))
End Select
Return output
End Function
Private Sub isFirst()
If isFirstExist = False Then
firstNum = CType(txtCalc.Text, Decimal)
isFirstExist = True
txtCalc.Text = "0"
End If
End Sub
Assuming you're using the textbox to hold the users input, in the Textchanged or even KeyPressed handler trap the operator keys and run the function instead of displaying the operator.
Something like this:
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtCalc.KeyPress
If e.KeyChar = "+"c Then
inputOperator = "+"
isFirst()
e.Handled = True
End If
End Sub
To display the operator set handled to false.
Public Class Form1
Dim num1 As Integer = CInt(Int((10 * Rnd()) + 1))
Dim num2 As Integer = CInt(Int((10 * Rnd()) + 1))
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
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
Label2.Text = "Correct!!11"
Else
Label2.Text = "Incorrect, sorry about that"
End If
End Sub
End Class
When i run this code, it only generates one question. which is 6*8. If i input 48, it works.But if i click the button again it will not generate another question. It will only generate 6*8. I need it to be able to generate random multiplication questions from 1-10
You're only generating num1 and num2 once, when the instance of the form is initialized, so every time you click the button, it's just reusing the same value.
You should generate a new values every time the button is clicked:
Dim num1 As Integer
Dim num2 As Integer
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
num1 = CInt(Int((10 * Rnd()) + 1))
num2 = CInt(Int((10 * Rnd()) + 1))
TextBox2.Text = num1 & "*" & num2
End Sub
If you get 6*8 every time you run the program, including completely stopping and rerunning,then that's really strange and I'm not sure what's up with that.
But if it's just that you're getting the same question every time you click the button, its because you put in a Rnd() when you declared the variables. If you want a new random number each time, you will have to put in some kind of loop where you reassign the variable each time.