calculation is only performed once on food ordering form (VB) - vb.net

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

Related

The input String is not in correct format even though my variable is an integer

Private Sub shirtnum_KeyPress(sender As Object, e As KeyPressEventArgs) Handles shirtnum.KeyPress ("this is suppose to type in any number using into the textbox using your keyboard")
Dim Ch As Char = e.KeyChar
If Not Char.IsDigit(Ch) AndAlso Asc(Ch) <> 8 Then
e.Handled = True
End If
End Sub
Private Sub addshbtn_Click(sender As Object, e As EventArgs) Handles addshbtn.Click
Dim t As Integer = Integer.Parse(shirtnum.Text) ("this is to add 1 number to the textbox")
t += 1
shirtnum.Text = t
addshbtn.Enabled = True
pricetxt_TextChanged(sender, e)
End Sub
Private Sub minusshbtn_Click(sender As Object, e As EventArgs) Handles minusshbtn.Click ("this is to minus 1 number from the textbox")
Dim t As Integer = Integer.Parse(shirtnum.Text)
t += -1
If t <= 0 Then
t = 0
End If
shirtnum.Text = t
pricetxt_TextChanged(sender, e)
End Sub
Private Sub shirtnum_TextChanged(sender As Object, e As EventArgs) Handles shirtnum.TextChanged
End Sub
Private Sub pricetxt_TextChanged(sender As Object, e As EventArgs) Handles pricetxt.TextChanged
"Dim t As Integer = Integer.Parse(shirtnum.Text) -- this is where the error appears **NOTE** this this code works without this sub"
t = 1.0
pricetxt.Text = ("$" & t * 15 & ".00")
pricetxt.BorderStyle = 0
pricetxt.BackColor = Me.BackColor
End Sub
Got an issue with my assignment is that the code appears to say that the input string is not in the correct format, even though my integer is a number.
If someone can help me out with this, this would be a big help, thanks!

Lock button when user put a specific value on one of the choices

I want to lock the other buttons when the user input an amount to one of the four rooms. Instead if I can't lock, please help me do the otherway whereas when the user click the other rooms, the other rooms will freeze to 0 value.
Public Class Formrooms
Dim birthday As Double
Dim party As Double
Dim vip As Double
Dim deluxe As Double
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lblparty.BackColor = ColorTranslator.FromHtml("#100F0E")
lblbday.BackColor = ColorTranslator.FromHtml("#100F0E")
lblvip.BackColor = ColorTranslator.FromHtml("#100F0E")
lbldeluxe.BackColor = ColorTranslator.FromHtml("#100F0E")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs)
Me.Hide()
Formsnacks.Show()
End Sub
Private Sub btnbirthday_Click(sender As Object, e As EventArgs)
Me.Hide()
Formsnacks.Show()
End Sub
Private Sub btnvip_Click(sender As Object, e As EventArgs)
Me.Hide()
Formsnacks.Show()
End Sub
Private Sub btndeluxe_Click(sender As Object, e As EventArgs)
Me.Hide()
Formsnacks.Show()
End Sub
Private Sub btndecp_Click(sender As Object, e As EventArgs) Handles btndecp.Click
lblparty.Text -= 1
If lblparty.Text <= 0 Then
lblparty.Text = 0
End If
End Sub
Private Sub btndecb_Click(sender As Object, e As EventArgs) Handles btndecb.Click
lblbday.Text -= 1
If lblbday.Text <= 0 Then
lblbday.Text = 0
End If
End Sub
Private Sub btndecv_Click(sender As Object, e As EventArgs) Handles btndecv.Click
lblvip.Text -= 1
If lblvip.Text <= 0 Then
lblvip.Text = 0
End If
End Sub
Private Sub btndecd_Click(sender As Object, e As EventArgs) Handles btndecd.Click
lbldeluxe.Text -= 1
If lbldeluxe.Text <= 0 Then
lbldeluxe.Text = 0
End If
End Sub
Private Sub btnincp_Click(sender As Object, e As EventArgs) Handles btnincp.Click
lblparty.Text += 1
If lblparty.Text >= 3 Then
lblparty.Text = 3
End If
End Sub
Private Sub btnincb_Click(sender As Object, e As EventArgs) Handles btnincb.Click
lblbday.Text += 1
If lblbday.Text >= 3 Then
lblbday.Text = 3
End If
End Sub
Private Sub btnincv_Click(sender As Object, e As EventArgs) Handles btnincv.Click
lblvip.Text += 1
If lblvip.Text >= 3 Then
lblvip.Text = 3
End If
End Sub
Private Sub btnincd_Click(sender As Object, e As EventArgs) Handles btnincd.Click
lbldeluxe.Text += 1
If lbldeluxe.Text >= 3 Then
lbldeluxe.Text = 3
End If
End Sub
Private Sub Number_only(sender As Object, e As KeyPressEventArgs)
If Asc(e.KeyChar) <> 8 Then
If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
e.Handled = True
End If
End If
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
Dim dialog As DialogResult
If lblbday.Text = 0 And lbldeluxe.Text = 0 And lblparty.Text = 0 And lblvip.Text = 0 Then
dialog = MessageBox.Show("You must choose the rooms", "Error", MessageBoxButtons.OK)
ElseIf lblbday.Text > 0 And lblparty.Text >= 1 And lblvip.Text = 1 And lbldeluxe.Text > 1 Then
dialog = MessageBox.Show("You can only choose 1 room", "Error", MessageBoxButtons.OK)
Else
Me.Hide()
formsnacks.Show()
End If
End Sub
Private Sub btnbackr_Click(sender As Object, e As EventArgs) Handles btnbackr.Click
Me.Hide()
Formtakeorders.Show()
End Sub
Private Sub lblparty_Click(sender As Object, e As EventArgs) Handles lblparty.Click
End Sub
End Class
If I've managed to pick out the actual meaning from your poor explanation, this type of thing will do the job:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button4.Click,
Button3.Click,
Button2.Click,
Button1.Click
For Each btn In Controls.OfType(Of Button)()
btn.Enabled = btn Is sender
Next
End Sub
One event handler for all Buttons and then set the Enabled property of each Button. sender is the object that raised the event, i.e. the Button that was clicked. All but that Button are not the sender so all but that Button will have their Enabled property set to False.
Note that this assumes that all those Buttons are in the same container - the form in this specific case - and that container contains no other Buttons. If that's not currently the case, you can either hard-code the list of Buttons to loop over or move just those Buttons into a new container, e.g. a Panel.
I think you should get rid of the four buttons (party, birthday, vip, deluxe) and just have ONE "next" button. Then just reset the other hour selections to zero whenever you increase one of the room values:
Public Class Formrooms
Dim party As Integer
Dim birthday As Integer
Dim vip As Integer
Dim deluxe As Integer
Private Sub btndecp_Click(sender As Object, e As EventArgs) Handles btndecp.Click
If party > 0 Then
party = party - 1
lblparty.Text = party
End If
End Sub
Private Sub btndecb_Click(sender As Object, e As EventArgs) Handles btndecb.Click
If birthday > 0 Then
birthday = birthday - 1
lblbday.Text = birthday
End If
End Sub
Private Sub btndecv_Click(sender As Object, e As EventArgs) Handles btndecv.Click
If vip > 0 Then
vip = vip - 1
lblvip.Text = vip
End If
End Sub
Private Sub btndecd_Click(sender As Object, e As EventArgs) Handles btndecd.Click
If deluxe > 0 Then
deluxe = deluxe - 1
lbldeluxe.Text = deluxe
End If
End Sub
Private Sub btnincp_Click(sender As Object, e As EventArgs) Handles btnincp.Click
party = Math.Min(party + 1, 3)
lblparty.Text = party
birthday = 0
vip = 0
deluxe = 0
lblbirthday.Text = birthday
lblvip.Text = vip
lbldeluxe.Text deluxe
End Sub
Private Sub btnincb_Click(sender As Object, e As EventArgs) Handles btnincb.Click
birthday = Math.Min(birthday + 1, 3)
lblbday.Text = birthday
party = 0
vip = 0
deluxe = 0
lblparty.Text = party
lblvip.Text = vip
lbldeluxe.Text deluxe
End Sub
Private Sub btnincv_Click(sender As Object, e As EventArgs) Handles btnincv.Click
vip = Math.Min(vip + 1, 3)
lblvip.Text = vip
party = 0
birthday = 0
deluxe = 0
lblparty.Text = party
lblbirthday.Text = birthday
lbldeluxe.Text = deluxe
End Sub
Private Sub btnincd_Click(sender As Object, e As EventArgs) Handles btnincd.Click
deluxe = Math.Min(deluxe + 1, 3)
lbldeluxe.Text = deluxe
party = 0
birthday = 0
vip = 0
lblparty.Text = party
lblbirthday.Text = birthday
lblvip.Text = vip
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles nextr.Click
If birthday = 0 AndAlso deluxe = 0 AndAlso party = 0 AndAlso vip = 0 Then
MessageBox.Show("You must choose a room with at least one hour.", "Error", MessageBoxButtons.OK)
Else
Me.Hide()
formsnacks.Show()
End If
End Sub
End Class
I don't understand exactly, but I think that's the solution.
[1]: https://i.stack.imgur.com/D62kl.png [example][1]
And I want an explanation of the idea you want to do if I don't help you with this solution
' Name my Button : Room1 , Room2 , Room3, Room4
'example Dim ButtonRoom As Button = CType(Me.Controls("Room" & j), Button)
' Name my Button : hello1 , hello2 , hello3, hello4
'example2 Dim ButtonRoom As Button = CType(Me.Controls("hello" & j), Button)
Private Sub Room1_Click(sender As Object, e As EventArgs) Handles Room1.Click
selected_room(1)
End Sub
Private Sub Room2_Click(sender As Object, e As EventArgs) Handles Room2.Click
selected_room(2)
End Sub
Private Sub Room3_Click(sender As Object, e As EventArgs) Handles Room3.Click
selected_room(3)
End Sub
Private Sub Room4_Click(sender As Object, e As EventArgs) Handles Room4.Click
selected_room(4)
End Sub
Public Function selected_room(ByVal i As Integer)
For j As Integer = 1 To 4
Dim ButtonRoom As Button = CType(Me.Controls("<name your button here>" & j), Button)
If j = i Then
ButtonRoom.Enabled = True
Else
ButtonRoom.Enabled = False
End If
Next j
Return True
End Function
If you do not benefit from the solution I hope you contact me
instagram : ovhc_
Vote for me if I've helped you.

Multiplication application, visual basic. invalid cast exception

Public Class Form1
Dim randomNumberOne As New Random
Dim randomNumberTwo As New Random
Dim ChildGrade As Integer
Dim strChildGrade As Integer
Dim strcorrectCounter As Integer
Dim Correctcounter As Integer = 0
Dim WrongCounter As Integer = 0
Dim totalQuestions As Integer = 0
Dim percent As Integer = 0
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ChildGrade = InputBox("Enter your age ", "age")
grade()
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberOne.Next(0, 12)
End Sub
Private Sub grade()
If ChildGrade <= 4 Then
Gradelbl.Text = "Preschool"
ElseIf ChildGrade = 5 Then
Gradelbl.Text = "Kindergarden"
ElseIf ChildGrade <= 10 Then
Gradelbl.Text = "Elementary"
ElseIf ChildGrade <= 13 Then
Gradelbl.Text = "Middle School"
ElseIf ChildGrade <= 18 Then
Gradelbl.Text = "High School"
ElseIf ChildGrade > 18 Then
Gradelbl.Text = "NA"
End If
End Sub
Private Sub RandomNumberToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles RandomNumberToolStripMenuItem.Click
NumbersGroupBox.Enabled = False
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End Sub
Private Sub SingleTableToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SingleTableToolStripMenuItem.Click
NumbersGroupBox.Enabled = True
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FirstNumberlbl.Text = "1"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
FirstNumberlbl.Text = "2"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
FirstNumberlbl.Text = "3"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
FirstNumberlbl.Text = "4"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
FirstNumberlbl.Text = "5"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
FirstNumberlbl.Text = "6"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
FirstNumberlbl.Text = "7"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
FirstNumberlbl.Text = "8"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
FirstNumberlbl.Text = "9"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
FirstNumberlbl.Text = "10"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
FirstNumberlbl.Text = "11"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
FirstNumberlbl.Text = "12"
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Private Sub CheckAnswer()
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
MsgBox("Please try again", MsgBoxStyle.Information, "Answer")
WrongCounter = WrongCounter + 1
If WrongCounter = 2 Then
MsgBox("The correct answer is " & FirstNumberlbl.Text * SecondNumberlbl.Text, MsgBoxStyle.Information, "Correct answer")
WrongCounter = 0
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
TextBox1.Text = ""
End If
End Sub
Private Sub rightAnswer()
If FirstNumberlbl.Text * SecondNumberlbl.Text = TextBox1.Text Then
Correctcounter = Correctcounter + 1
NumberCorrectlbl.Text = Correctcounter
TextBox1.Text = ""
FirstNumberlbl.Text = randomNumberOne.Next(0, 12)
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
End If
totalQuestions = totalQuestions + 1
End Sub
Private Sub Enterbtn_Click(sender As Object, e As EventArgs) Handles Enterbtn.Click
CheckAnswer()
rightAnswer()
TotalCountlbl.Text = totalQuestions
PercentCorrectlbl.Text = ((Correctcounter * 100) / (totalQuestions * 100)) * 100
End Sub
End Class
I'm creating a multiplication application in VB that keeps count of how many questions the user gets right and how many they get wrong. When I run the program, it works fine in counting the amount right and totaling up the percentage, but when I test for when they get it wrong I get this error. enter image description here
if the image isn't visible, the error says,
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "" to type 'Double' is not valid
The program points to the problem being in the if statement in the rightAnswer() sub class but i'm not exactly sure what is wrong with it.
The problem is you are trying to do calculations with strings. Strings are text, not numbers.
This is not a full answer but enough to get you to understand the problem and change your code to fix it.
Don't do any calculations on the contents of text boxes and/or labels. Use the correct variables for such tasks. Use the controls for simply retrieving and displaying. This will save you a lot of heart-ache in the long run.
So for example, don't do:
If TextBox1.Text <> (FirstNumberlbl.Text * SecondNumberlbl.Text) Then
You're checking a number in the TextBox to a calculation based on two labels, they are all text!
So think along the lines of doing calculations on a variable fit for purpose, an integer for example:
Example:
Dim FirstNumber As Integer
Dim SecondNumber As Integer
Use these for the calculations, then, display the result as text.
If you are expecting a floating point number, a decimal, change the variable type as needed.
In your 'CheckAnswer' subroutine you should starting thinking along the lines of:
If Not String.IsNullOrWhiteSpace(TextBox1.Text) Then
'We have something in the TextBox (you can check to allow only numbers to be entered in the TextBox.KeyDown/Press event)
If Not Integer.Parse(TextBox1.Text) = (FirstNumber * SecondNumber) Then
'Do what you need ...
End If
End If
There's a couple of other things too that could simplify your code.
You don't need 12 button events all doing the same thing.
In you Form1_Load Event put this:
Dim bTags() As Button = {Button1, Button2, Button3, Button4, Button5, Button6, Button7, Button8, Button9, Button10, Button11, Button12}
For t As Integer = 0 To bTags.Count - 1
bTags(t).Tag = t + 1
Next
Then delete all your 12 Button Click events and replace them with:
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button8.Click, Button7.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click, Button12.Click, Button11.Click, Button10.Click, Button1.Click
Dim selectedButton As Button = CType(sender, Button)
FirstNumberlbl.Text = selectedButton.Tag.ToString
SecondNumberlbl.Text = randomNumberTwo.Next(0, 12)
TextBox1.Focus()
End Sub
Job done...
Also, you grade subroutine would be better of as a Select Case function:
In your Form1_Load replace: grade() with Gradelbl.Text = Grade() and your grade() subroutine replaced with:
Private Function Grade() As String
Select Case ChildGrade
Case < 5 : Return "Preschool"
Case = 5 : Return "Kindergarden"
Case < 11 : Return "Elementary"
Case < 14 : Return "Middle School"
Case < 19 : Return "High School"
Case Else : Return "N/A"
End Select
End Function
I hope some of the advice will be of help.
What you need to do is cast the values of the textbox. For example:
Convert.toInt32(FirstNumberlbl.Text)
or
Dim firstNumber as Integer
Integer.TryParse(FirstNumberlbl.Text, firstNumber)
Or assign a variable to the textbox and cast the variable instead of the textbox.

Add items to listview after cleared

I am adding all the textboxes and labels to display in the listview. When I click the clear button everything on my form clears as it should, but when I then want to add more items to the listview, nothing displays in the listview, and my header information is also cleared. Can someone please assist?
Public Class Form2
Dim decTotalDue As Decimal
Dim intTotalItems As Integer
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles txtUnitPrice.TextChanged
End Sub
Private Sub btnAddItem_Click(sender As Object, e As EventArgs) Handles btnAddItem.Click
Dim decUnitPrice As Decimal
Dim intQuantity As Integer
Dim decTotal As Decimal
Dim decTotalPayable As Decimal
Dim item As New ListViewItem
Decimal.TryParse(txtUnitPrice.Text, decUnitPrice)
Integer.TryParse(txtQuantity.Text, intQuantity)
decTotal = decUnitPrice * intQuantity
lblTotal.Text = decTotal.ToString("C2")
decTotalDue = decTotal + decTotalDue
lblTotalDue.Text = decTotalDue.ToString("C2")
intTotalItems = intQuantity + intTotalItems
lblTotalItems.Text = intTotalItems.ToString
decTotalPayable = decTotalDue
lblTotalPayable.Text = decTotalPayable.ToString("C2")
lblTotalPayable.Hide()
lblTotalItems.Hide()
item = ListView1.Items.Add(cboItemName.Text)
item.SubItems.Add(txtUnitPrice.Text)
item.SubItems.Add(txtQuantity.Text)
item.SubItems.Add(lblTotal.Text)
ListView1.ForeColor = Color.White
txtUnitPrice.Text = decUnitPrice.ToString("C2")
End Sub
Private Sub btnPurchase_Click(sender As Object, e As EventArgs) Handles btnPurchase.Click
lblTotalItems.Show()
lblTotalPayable.Show()
cboItemName.Text = String.Empty
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotal.Text = ""
lblTotalDue.Text = ""
ListView1.Clear()
End Sub
Private Sub btnCalculateChange_Click(sender As Object, e As EventArgs) Handles btnCalculateChange.Click
Dim decCashTenderted As Decimal
Dim decChange As Decimal
Decimal.TryParse(txtCashTendered.Text, decCashTenderted)
txtCashTendered.Text = decCashTenderted.ToString("C2")
decChange = decCashTenderted - decTotalDue
lblChange.Text = decChange.ToString("C2")
If decCashTenderted < decTotalDue Then
MessageBox.Show("Cash Tendered is less than Total Due", "Invalid", MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
End If
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
cboItemName.Text = String.Empty
txtCashTendered.Clear()
txtUnitPrice.Clear()
txtQuantity.Clear()
lblTotalDue.Text = ""
lblTotalItems.Text = ""
lblTotalPayable.Text = ""
lblChange.Text = ""
ListView1.Clear()
End Sub
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
End Class
MSDN on the behaviour of the Clear function is:
You can use this method to remove all items and columns from the ListView control without having to call the individual Clear methods from the ListView.ColumnHeaderCollection and ListView.ListViewItemCollection classes.
From what you describe you want, you should be doing is calling:
ListView1.Items.Clear()
This will remove just the items that are displayed and not remove the column definitions.

How to add/minus multiple checkbox values

I'm using VB.net and I'm having trouble trying to add different values when I select checkboxes and diplaying the total in a textbox. When unchecking them, it should be subtracting the values. Here is my code
"totals" is my textbox
Dim total As Double
Private Sub cchstk_CheckStateChanged(sender As Object, e As EventArgs) Handles cchstk.CheckStateChanged
If (cchstk.Checked = True) Then
total = total + 109.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 109.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub cms_CheckStateChanged(sender As Object, e As EventArgs) Handles cms.CheckStateChanged
If (cms.Checked = True) Then
total = total + 79.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 79.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub prrdg_CheckStateChanged(sender As Object, e As EventArgs) Handles prrdg.CheckStateChanged
If (prrdg.Checked = True) Then
total = total + 49.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 49.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub gb_CheckStateChanged(sender As Object, e As EventArgs) Handles gb.CheckStateChanged
If (gb.Checked = True) Then
total = total + 29.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 29.99
totals.Text = Double.Parse(total)
End If
End Sub
Private Sub nd_CheckStateChanged(sender As Object, e As EventArgs) Handles nd.CheckStateChanged
If (nd.Checked = True) Then
total = total + 29.99
totals.Text = Double.Parse(total)
ElseIf (chstk.Checked = False) Then
total = total - 29.99
totals.Text = Double.Parse(total)
End If
End Sub`
you only have to change all your Double.Parse(total) to total.ToString
you want to convert a number to string, not a number to a number.
also do not use double if you can, you will be hit by floating conversion issue
use decimal and you can also create a method to reduce the repetition of code
Dim total As Decimal
Private Sub ChangeValue(add As Boolean, value As Decimal)
total += If(add, value, -value)
TextBox1.Text = total.ToString
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 109.99D)
End Sub
Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 79.99D)
End Sub
Private Sub CheckBox3_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox3.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 49.99D)
End Sub
Private Sub CheckBox4_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox4.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub
Private Sub CheckBox5_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox5.CheckedChanged
ChangeValue(DirectCast(sender, CheckBox).Checked, 29.99D)
End Sub