Help with very basic coding
Hello everyone. I have been assigned as a "Final" to code a Visual Basic program that takes concession stand orders, adds the items to a list, then figures the change.
This is a picture of the interface I designed to give you all the right idea.
http://img7.imageshack.us/img7/8553/50832447.jpg
Code:
Private Sub xAddHamburgerButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xAddHamburgerButton.Click
Dim total As Double
Double.TryParse(Me.xTotalLabel.Text, total)
total = total + 2
Me.xTotalLabel.Text = total.ToString("C2")
Me.xItemsListBox.Items.Add("Hamburger $2.00")
End Sub
This code works to a point. It adds the $2.00 in the total box, but keeps it there even if I press the "add" button next to "hamburger" again. I would like that total to stack every time I press the button so it keeps a running total of everything purchased(and go on when I add other items, like "pizza" to the listbox).
Also this code isn't doing what is desired.
Code:
Private Sub xChangeButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles xChangeButton.Click
Dim amount As Integer
Dim total As Integer
Dim change As Integer
Integer.TryParse(Me.xAmountRecievedTextBox.Text, amount)
Integer.TryParse(Me.xTotalLabel.Text, total)
Integer.TryParse(Me.xChangeLabel.Text, change)
change = amount - total
Me.xChangeLabel.Text = change.ToString("C2")
End Sub
This picture demonstrates what happens: http://img15.imageshack.us/img15/8686/vb2w.jpg
And one last thing. What code would I use to make it remove a selected item from the list box? (xItemsListBox)
Any help would be greatly appreciated. Sorry if I sound like a newbie... but I am. :)
Re: Help with very basic coding
for your add to total button:
vb Code:
Double.TryParse(Me.xTotalLabel.Text.replace("$",""), total)
for your change button, integers are whole numbers. you need to use doubles or decimals.
Re: Help with very basic coding
Code:
This code works to a point. It adds the $2.00 in the total box, but keeps it there even if I press the "add" button next to "hamburger" again. I would like that total to stack every time I press the button so it keeps a running total of everything purchased(and go on when I add other items, like "pizza" to the listbox).
after each button click for adding or subtracting, and modifying the variable, set the label to the value again.
vb Code:
total = total + <amount here>
labelnamehere.text = total
Re: Help with very basic coding
Quote:
Originally Posted by
formlesstree4
Code:
This code works to a point. It adds the $2.00 in the total box, but keeps it there even if I press the "add" button next to "hamburger" again. I would like that total to stack every time I press the button so it keeps a running total of everything purchased(and go on when I add other items, like "pizza" to the listbox).
after each button click for adding or subtracting, and modifying the variable, set the label to the value again.
vb Code:
total = total + <amount here>
labelnamehere.text = total
That mostly works, but I need it do display in currency. Still closer than I was.
Thanks :)
Edit: Never mind. It works now. I just accidentally deleted a line.
Re: Help with very basic coding
Decimal will work better than either integer or double for dealing with currency. Not sure if you still have a question, though.
I also noted that you were declaring total as a local variable in the sub, which means that each time the sub is entered, the variable will be reset to 0, so there isn't any point in adding 2 to it. You might as well just set it to 2. It certainly appears that total should be declared outside of the sub at form scope.
Re: Help with very basic coding
You do know that your "total" variable will not hold any decimal numbers? Meaning if you add a hamburger that is 1.50 to it, then the total will read 2 because Integers don't hold decimals so they automatically round.
Instead, make all of your number variables Decimals, then when you want to output the total, use:
Code:
total.ToString("C2")
And it will display in US Currency mode. Also, I suggest you turn Option Strict On to avoid conversion errors.
Re: Help with very basic coding
Quote:
Originally Posted by
Vectris
You do know that your "total" variable will not hold any decimal numbers? Meaning if you add a hamburger that is 1.50 to it, then the total will read 2 because Integers don't hold decimals so they automatically round.
Instead, make all of your number variables Decimals, then when you want to output the total, use:
Code:
total.ToString("C2")
And it will display in US Currency mode. Also, I suggest you turn Option Strict On to avoid conversion errors.
Ah! Of course! Thank you much. I'm not entirely sure how I over looked that. :)
Quote:
Originally Posted by
Shaggy Hiker
Decimal will work better than either integer or double for dealing with currency. Not sure if you still have a question, though.
I also noted that you were declaring total as a local variable in the sub, which means that each time the sub is entered, the variable will be reset to 0, so there isn't any point in adding 2 to it. You might as well just set it to 2. It certainly appears that total should be declared outside of the sub at form scope.
I did have one more question in the post about removing items from the list box. It need to be set up that when you remove an item from a list box, the cost of that item also subtracts from the total. Any ideas?
Sorry about all the questions. :/
Re: Help with very basic coding
Will the cost always be at the end like this?
Hamburger $2.00
You can use IndexOf and pull it out of the text like
Code:
Dim Cost as Decimal
Dim theItem as String = ListBox1.SelectedText
ListBox1.Items.Remove(ListBox1.SelectedIndex)
Cost = CDec(theText.Substring(theText.IndexOf("$") + 1))
And then subtract Cost from your total.
Re: Help with very basic coding
Simple Idea:
Use two labels for the total cost:
1 label is the $ symbol
the other is the actual amount. Would make stuff alot easier.
As for subtraction:
vb Code:
value = value - amounthere
labelname.text = value
Re: Help with very basic coding
Here's an example of some code I just tested for an add and remove button:
vb Code:
Private Sub chsburgrAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrAdd.Click
foodListBox.Items.Add("Cheeseburger: $2.50")
totalMoney += 2.5
finalCostLbl.Text = "$" & totalMoney
End Sub
Private Sub chsburgrDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrDel.Click
Try
If foodListBox.Items.Contains("Cheeseburger: $2.50") = True Then
foodListBox.Items.Remove("Cheeseburger: $2.50")
If totalMoney <= 0 Then
totalMoney = 0.0
finalCostLbl.Text = "$" & totalMoney
Else
totalMoney -= 2.5
finalCostLbl.Text = "$" & totalMoney
End If
Else
Exit Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Very rough, but you get the idea of what I mean.
EDIT: 2 things
1. I don't care if its not Option Strict coding.
2. I wrapped it in Try statements because I was guessing my work.
Re: Help with very basic coding
Quote:
Originally Posted by
formlesstree4
Here's an example of some code I just tested for an add and remove button:
vb Code:
Private Sub chsburgrAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrAdd.Click
foodListBox.Items.Add("Cheeseburger: $2.50")
totalMoney += 2.5
finalCostLbl.Text = "$" & totalMoney
End Sub
Private Sub chsburgrDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrDel.Click
Try
If foodListBox.Items.Contains("Cheeseburger: $2.50") = True Then
foodListBox.Items.Remove("Cheeseburger: $2.50")
If totalMoney <= 0 Then
totalMoney = 0.0
finalCostLbl.Text = "$" & totalMoney
Else
totalMoney -= 2.5
finalCostLbl.Text = "$" & totalMoney
End If
Else
Exit Sub
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Very rough, but you get the idea of what I mean.
EDIT: 2 things
1. I don't care if its not Option Strict coding.
2. I wrapped it in Try statements because I was guessing my work.
This code is amazing to me, and it almost exactly what I wanted. I may be messing it up though, but when I do it if I have more than one cheese burger (say, 3 of them for a total cost of $7.50) the first time it will subtract properly to $5.00, then the second time it will go straight to zero.
Thank you every one who's helped me so far, though. I really appreciate it.
Re: Help with very basic coding
Make sure you aren't skipping any lines. This code doesn't mess up, I've tested it now, using hamburgers too (at $2 dollars) and mixing things up. It appeared to work.
Experiment around with it, see if you can figure out whats going on.
Re: Help with very basic coding
One flaw I see with your UI design, and maybe others will agree, is that you've actually designed the form with the available items on it. The point I'm trying to make is that if the "Concession Stand" adds new items, then you have to go in and add the buttons, the code behind the buttons etc... In OOP design you really want to make stuff like that easy on yourself.
IMO this situation is perfect for a Dictionary(Of String, Decimal). Use two ListBoxes: 1 shows the available items, the other shows what they've chosen. Splitting these two ListBoxes are the [+]/[-] buttons. This is the code I used, I didn't do the amount received, change part. I went up to the subtotal step:
Code:
Public Class Form1
Private ConItems As New Dictionary(Of String, Decimal)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
With ConItems
.Add("Hamburger (Price: $2.50)", 2.5D)
.Add("Hot Dog (Price: $2.00)", 2D)
.Add("Chips (Price: $1.25)", 1.25D)
'Add items here
End With
Dim Items(ConItems.Count - 1) As String
ConItems.Keys.CopyTo(Items, 0)
Me.ltbItemsAvailable.DataSource = Items
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
Me.ltbItemsChosen.Items.Add(Me.ltbItemsAvailable.SelectedItem)
Me.lblSubTotal.Text = "Sub Total: $ " & SubTotal.ToString
End Sub
Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
If Me.ltbItemsChosen.SelectedIndex <> -1 Then
Me.ltbItemsChosen.Items.RemoveAt(Me.ltbItemsChosen.SelectedIndex)
End If
Me.lblSubTotal.Text = "Sub Total: $ " & SubTotal.ToString
End Sub
Private Function SubTotal() As Decimal
Dim SubTotalPrice As Decimal = 0D
For Each ConItem As String In Me.ltbItemsChosen.Items
SubTotalPrice += ConItems(ConItem.ToString)
Next
Return SubTotalPrice
End Function
End Class
If the Concession Stand adds new items, all you have to do is go to the dictionary and add the item and the associated price. I don't know if your instructor is ok with you using a DataSource, if you're unsure I wouldn't use this code...
Re: Help with very basic coding
Quote:
Originally Posted by
Vectris
Will the cost always be at the end like this?
Hamburger $2.00
You can use IndexOf and pull it out of the text like
Code:
Dim Cost as Decimal
Dim theItem as String = ListBox1.SelectedText
ListBox1.Items.Remove(ListBox1.SelectedIndex)
Cost = CDec(theText.Substring(theText.IndexOf("$") + 1))
And then subtract Cost from your total.
Ummm, have you tried my code??? It's a lot shorter and it will only delete the selected item, not any others even if they are the same item.
Re: Help with very basic coding
Quote:
Originally Posted by
Vectris
Ummm, have you tried my code??? It's a lot shorter and it will only delete the selected item, not any others even if they are the same item.
Ummm, have you read the documentation for .remove?
.removeat removes by index