Results 1 to 15 of 15

Thread: Help with very basic coding

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    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.


    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:

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with very basic coding

    for your add to total button:

    vb Code:
    1. Double.TryParse(Me.xTotalLabel.Text.replace("$",""), total)

    for your change button, integers are whole numbers. you need to use doubles or decimals.

  3. #3

    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:
    1. total = total + <amount here>
    2. labelnamehere.text = total

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    Re: Help with very basic coding

    Quote Originally Posted by formlesstree4 View Post
    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:
    1. total = total + <amount here>
    2. 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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    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.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    Re: Help with very basic coding

    Quote Originally Posted by Vectris View Post
    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 View Post
    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. :/
    Last edited by TiffanyP; May 1st, 2009 at 12:24 AM.

  8. #8
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  9. #9

    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:
    1. value = value - amounthere
    2. labelname.text = value

  10. #10

    Re: Help with very basic coding

    Here's an example of some code I just tested for an add and remove button:

    vb Code:
    1. Private Sub chsburgrAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrAdd.Click
    2.         foodListBox.Items.Add("Cheeseburger: $2.50")
    3.         totalMoney += 2.5
    4.         finalCostLbl.Text = "$" & totalMoney
    5.     End Sub
    6.  
    7.     Private Sub chsburgrDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrDel.Click
    8.         Try
    9.             If foodListBox.Items.Contains("Cheeseburger: $2.50") = True Then
    10.                 foodListBox.Items.Remove("Cheeseburger: $2.50")
    11.                 If totalMoney <= 0 Then
    12.                     totalMoney = 0.0
    13.                     finalCostLbl.Text = "$" & totalMoney
    14.                 Else
    15.                     totalMoney -= 2.5
    16.                     finalCostLbl.Text = "$" & totalMoney
    17.                 End If
    18.             Else
    19.                 Exit Sub
    20.             End If
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         End Try
    24.     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.

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    4

    Re: Help with very basic coding

    Quote Originally Posted by formlesstree4 View Post
    Here's an example of some code I just tested for an add and remove button:

    vb Code:
    1. Private Sub chsburgrAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrAdd.Click
    2.         foodListBox.Items.Add("Cheeseburger: $2.50")
    3.         totalMoney += 2.5
    4.         finalCostLbl.Text = "$" & totalMoney
    5.     End Sub
    6.  
    7.     Private Sub chsburgrDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chsburgrDel.Click
    8.         Try
    9.             If foodListBox.Items.Contains("Cheeseburger: $2.50") = True Then
    10.                 foodListBox.Items.Remove("Cheeseburger: $2.50")
    11.                 If totalMoney <= 0 Then
    12.                     totalMoney = 0.0
    13.                     finalCostLbl.Text = "$" & totalMoney
    14.                 Else
    15.                     totalMoney -= 2.5
    16.                     finalCostLbl.Text = "$" & totalMoney
    17.                 End If
    18.             Else
    19.                 Exit Sub
    20.             End If
    21.         Catch ex As Exception
    22.             MsgBox(ex.Message)
    23.         End Try
    24.     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.

  12. #12

    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.

  13. #13
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    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...
    Last edited by ForumAccount; May 1st, 2009 at 09:16 PM.

  14. #14
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: Help with very basic coding

    Quote Originally Posted by Vectris View Post
    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.
    If your problem is solved, click the Thread Tools button at the top and mark your topic as Resolved!

    If someone helped you out, click the button on their post and leave them a comment to let them know they did a good job

    __________________
    My Vb.Net CodeBank Submissions:
    Microsoft Calculator Clone
    Custom TextBox Restrictions
    Get the Text inbetween HTML Tags (or two words)

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Help with very basic coding

    Quote Originally Posted by Vectris View Post
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width