I am very new to VS 2012 and I'm wondering how I can save what a user has inputted so that it's there when they re-open the program. Right now I just have 2 buttons that increase a label by 1 each time you press them and another label that divides one by the other into a per cent.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim k, d, r As Single
k = Label2.Text + 1
d = Label4.Text
r = (d / k)
Label2.Text = k
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim k, d, r As Single
k = Label2.Text
d = Label4.Text + 1
r = (d / k)
Label4.Text = d
Label6.Text = Format(r, "Percent")
End Sub
It your case you have to save your input to some external file, and on reopen program to read that file and to init your fiels (labels, textboxes ets)
Good tutorial about read write from-to files using vb here
Use Application Settings to store the values.
Go to Project --> Properties --> Settings Tab.
Add entries for "Label2", "Label4", and "Label6", leaving the Type as String:
Now add code to the Load() and FormClosing() events of your Form to load and save the values to/from your Application Settings:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If My.Settings.Label2 <> "" Then
Label2.Text = My.Settings.Label2
End If
If My.Settings.Label4 <> "" Then
Label4.Text = My.Settings.Label4
End If
If My.Settings.Label6 <> "" Then
Label6.Text = My.Settings.Label6
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim k, d, r As Single
k = Label2.Text + 1
d = Label4.Text
r = (d / k)
Label2.Text = k
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim k, d, r As Single
k = Label2.Text
d = Label4.Text + 1
r = (d / k)
Label4.Text = d
Label6.Text = Format(r, "Percent")
End Sub
Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
My.Settings.Label2 = Label2.Text
My.Settings.Label4 = Label4.Text
My.Settings.Label6 = Label6.Text
My.Settings.Save()
End Sub
End Class
Related
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
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.
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.
I am making a food ordering form, and I am having trouble with the calculations. Every time a hotdog is ordered the form is supposed to add the cost to the total amount, so one hotdog is 2 dollars, add another and it says 4 dollars, etc. But it only performs the calculation once, so no matter how many items I add to the order the price stays the same, as if I had only ordered one.
I don't get any errors at runtime so it must be a logical error. Here is the full code. I don't know what other information I should give at this moment. All help is appreciated.
Public Class DroneDogs
Dim DogChoice As String
Dim condiment1 As String
Dim condiment2 As String
Dim condiment3 As String
Const DBL_TAX_RATE As Double = 0.07
Const DBL_DRONE_DOG As Double = 1.99
Dim intNumDog As Integer
Dim dblSubTotal As Double
Dim dblSalesTax As Double
Dim dblTotalCost As Double
Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
cboCurrentCustomers.Items.Add(txtAddCustomer.Text)
End Sub
Private Sub optBeef_CheckedChanged(sender As Object, e As EventArgs) Handles optBeef.CheckedChanged
If (optBeef.Checked) Then
DogChoice = "Beef Dog"
End If
End Sub
Private Sub optPork_CheckedChanged(sender As Object, e As EventArgs) Handles optPork.CheckedChanged
If (optPork.Checked) Then
DogChoice = "Pork Dog"
End If
End Sub
Private Sub optTurkey_CheckedChanged(sender As Object, e As EventArgs) Handles optTurkey.CheckedChanged
If (optTurkey.Checked) Then
DogChoice = "Turkey Dog"
End If
End Sub
Private Sub chkKetchup_CheckedChanged(sender As Object, e As EventArgs) Handles chkKetchup.CheckedChanged
If (chkKetchup.Checked) Then
condiment1 = " + Ketchup"
Else
condiment1 = Nothing
End If
End Sub
Private Sub chkMustard_CheckedChanged(sender As Object, e As EventArgs) Handles chkMustard.CheckedChanged
If (chkMustard.Checked) Then
condiment2 = " + Mustard"
Else
condiment2 = Nothing
End If
End Sub
Private Sub chkRelish_CheckedChanged(sender As Object, e As EventArgs) Handles chkRelish.CheckedChanged
If (chkRelish.Checked) Then
condiment3 = " + Relish"
Else
condiment3 = Nothing
End If
End Sub
Private Sub btnAddDog_Click(sender As Object, e As EventArgs) Handles btnAddDog.Click
intNumDog = +1
dblSubTotal = intNumDog * DBL_DRONE_DOG
dblSalesTax = dblSubTotal * DBL_TAX_RATE
dblTotalCost = dblSubTotal + dblSalesTax
txtSub.Text = dblSubTotal.ToString("c2")
txtTax.Text = dblSalesTax.ToString("c2")
txtTotal.Text = dblTotalCost.ToString("c2")
Dim addCondiment As String = condiment1 + condiment2 + condiment3
If (DogChoice = "Beef Dog") Or
(DogChoice = "Pork Dog") Or
(DogChoice = "Turkey Dog") Then
lstOrder.Items.Add(DogChoice + addCondiment + ": " + txtTotal.Text)
Else
MsgBox("Please select a Hot Dog type.")
End If
End Sub
Private Sub btnClearOrder_Click(sender As Object, e As EventArgs) Handles btnClearOrder.Click
lstOrder.Items.Clear()
txtSub.Clear()
txtTax.Clear()
txtTotal.Clear()
chkKetchup.Checked = 0
chkMustard.Checked = 0
chkRelish.Checked = 0
optBeef.Checked = 0
optPork.Checked = 0
optTurkey.Checked = 0
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
'Exits the program
If MessageBox.Show("Do you want to exit the DroneDogs application?", "DroneDogs",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) _
= DialogResult.Yes Then
Application.Exit()
End If
End Sub
Private Sub btnOrderComplete_Click(sender As Object, e As EventArgs) Handles btnOrderComplete.Click
Receipt.Show()
End Sub
Private Sub btnRemoveDog_Click(sender As Object, e As EventArgs) Handles btnRemoveDog.Click
lstOrder.Items.Remove(lstOrder.SelectedItem)
End Sub
End Class
I think the problem is with the line
intNumDog = +1
In this line, you always set intNumDog to the value of +1. This is the same as:
intNumDog = 1
You want to add one to the value of intNumDog:
intNumDog += 1
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