How to store last button clicked and add to listbox - vb.net

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

Related

VB Calculator add some functions

I have programmed a calculator with Visual Basic, but I would like to add a few more functions. But I can't figure out how to solve the following problems in my code:
if more than one zero is entered in the input field, it should simply delete the leading zeros.(example 0000111 -----> 111 or 0002345 ----> 2345)
likewise, a decimal number should not have several zeros before the decimal point (example 000.176 ----> 0.176)
furthermore, it should not be possible to enter two commas (Example 0.187.56 -----> should not be Possible -------------> 0.18756)
Could somebody help me ?
Public Class Form1
Public LCD As Single
Public OPZ As String
Private Sub LCDfuellen(sender As Object, e As EventArgs) Handles btn3.Click, btn2.Click, btn1.Click, btn9.Click, btn8.Click, btn7.Click, btn6.Click, btn5.Click, btn4.Click
txtLCD.Text &= ActiveControl.Tag
Replace(LTrim(Replace(txtLCD.Text, "0", " ")), " ", "0")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
txtLCD.Clear()
End Sub
Private Sub btnCE_Click(sender As Object, e As EventArgs) Handles btnCE.Click
'txtLCD.Text = txtLCD.Text.Remove(txtLCD.TextLength - 1, 1)
txtLCD.Text = LSet(txtLCD.Text, txtLCD.TextLength - 1)
End Sub
Private Sub OPZlesen(sender As Object, e As EventArgs) Handles btnPlus.Click, btnMinus.Click, btnMal.Click, btnDurch.Click
LCD = CSng(txtLCD.Text)
txtLCD.Clear()
OPZ = ActiveControl.Tag
End Sub
Private Sub btnGleich_Click(sender As Object, e As EventArgs) Handles btnGleich.Click
Dim activeLCD As Single
activeLCD = CSng(txtLCD.Text)
Select Case OPZ
Case "+" : txtLCD.Text = CStr(LCD + activeLCD)
Case "-" : txtLCD.Text = CStr(LCD - activeLCD)
Case "*" : txtLCD.Text = CStr(LCD * activeLCD)
Case "/" : txtLCD.Text = CStr(LCD / activeLCD)
End Select
End Sub
Private Sub btn0_Click(sender As Object, e As EventArgs) Handles btn0.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
Private Sub btnPunkt_Click(sender As Object, e As EventArgs) Handles btnPunkt.Click
txtLCD.Text &= ActiveControl.Tag
End Sub
End Class

How to clear Texboxes attached with message boxes in vb.net

For Learning the coding I built this application. It has Three text boxes.1 and 2 to enter numbers and 3 is to sum of 1 and 2. Button 2 is to get sum and 1 to clear textboxes.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
End Sub
End Class
Here I have limited the TextBox1 to get numbers between 0 and 100. if it is over 100, displays a warning massage box.
When I click the clear button it gives an error. It doesn't clear. after deleting textbox 1 clearing code it works fine. I mean textbox 2 & 3 clear fine. There is a problem with Textbox 1. The reason i believe is the msgbox attached it. I need to keep Msgbox.
How do I clear them?
You need to suppress text changed event. See TextBoxBase.Clear Method
Public Class Form1
Private flag As Boolean
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
' Check the flag to prevent code re-entry.
If flag = False Then
' Set the flag to True to prevent re-entry of the code below.
If TextBox1.Text >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox3.Text = Int(TextBox1.Text) + Int(TextBox2.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
flag = True
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
flag = False
End Sub
End Class
TextBox1.Clear() will fire TextBox1_TextChanged().
This line will do an implicit conversion from text to integer - it will fail with an error on blank or text entries:
If TextBox1.Text >= 101 Then
Instead, try this:
Dim number As Integer
If Int32.TryParse(TextBox1.Text, number) Then
If number >= 101 Then
MsgBox("Enter numbers between 0 and 100")
End If
End If

Input Strings into String Arrays(Dynamic)

So I have two string arrays, one for my friends, the other for their numbers. I go to my combo box (displays list of names) I click on it and it displays their number on a label. My problem is I want the user to a enter a new name and number in 2 textboxes, then transfer the name on the combo box. Once their name is in the box, I click on it and it display's their number on label. Is there a way to transfer the new items onto my arrays?
Option Explicit On
Module MainModule
Public strPeople() As String = {"Kyle", "John", "Jake", "Donna", "Carly", "Ty", "Mavis"}
Public strPhoneNumbers() As String = {"945-1232", "804-2329", "290-7321", "928-4569", "205-9893", "320-0195", "305-4520"}
Public tempList As New List(Of String)
End Module
Here Is My Main Form
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(strPeople)
End Sub
Private Sub AboutToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles AboutToolStripMenuItem1.Click
AboutBox1.ShowDialog()
End Sub
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
Application.Exit()
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click
End Sub
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim strPhoneNums As String = strPhoneNumbers(ComboBox1.SelectedIndex)
Label3.Text = "Phone Number: " & strPhoneNums
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
//Add Contact Button
If TextBox1.Text <> "" Then
ReDim Preserve strPeople(7)
strPeople(7) = TextBox1.Text
ComboBox1.Items.Add(strPeople(7))
End If
If TextBox2.Text <> "" Then
ReDim Preserve strPhoneNumbers(7)
strPhoneNumbers(7) = TextBox2.Text
End If
TextBox1.Clear()
TextBox2.Clear()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Application.Exit()
End Sub

calculation is only performed once on food ordering form (VB)

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

Error Message TryParse

I am struggling with my TryParse method. The program is to calculate the total rainfall and average rainfall. User enters each month in inches. The totals are accurate, but the error message "enter numeric value" pops up when you click calculate. The program also calculates with a symbol or letter, how would i go about stopping it at that point.
Public Class Korszun
Private Sub Korszun_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MessageBox.Show("Please enter each month's rainfall average (ex: 1.11 inches)")
End Sub
Private Sub txtJuly_TextChanged(sender As Object, e As EventArgs) Handles txtJuly.TextChanged
End Sub
Private Sub txtOctober_TextChanged(sender As Object, e As EventArgs) Handles txtOctober.TextChanged
End Sub
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'declare the variables
Dim intCount As Integer = 0
Dim dblAverageRainfall As Double
Dim dblTotalRainfall As Double
Dim dblInches As Double
'create txtbox variable
'go through each TextBox control in the Groupbox and add them all up
For Each txtBox As TextBox In Me.GroupBox1.Controls.OfType(Of TextBox)()
If Not Double.TryParse(txtBox.Text, dblInches) Then
MessageBox.Show("Please enter numerical values only.")
ElseIf CDbl(txtBox.Text) < 0 Then
MessageBox.Show("Please enter all positive numbers.")
ElseIf TypeOf txtBox Is TextBox Then
'add the total value of rainfall and put it in the decTotalRainfall variable
dblTotalRainfall += CDbl(txtBox.Text)
End If
Next
'get the average
dblAverageRainfall = dblTotalRainfall / 12
'show the results in the labels
lblTotal.Text = dblTotalRainfall.ToString("n2")
lblAverage.Text = dblAverageRainfall.ToString("n5")
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
'Clear the Textboxes
txtJanuary.Clear()
txtFebruary.Clear()
txtMarch.Clear()
txtApril.Clear()
txtMay.Clear()
txtJune.Clear()
txtJuly.Clear()
txtAugust.Clear()
txtSeptember.Clear()
txtOctober.Clear()
txtNovember.Clear()
txtDecember.Clear()
lblAverage.Clear()
lblTotal.Clear()
'Set focus
txtJanuary.Focus()
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub txtAverage_TextChanged(sender As Object, e As EventArgs) Handles lblAverage.TextChanged
End Sub
End Class