Can someone shorten this code using a wildcard?

12padams

Well-known member
Joined
Feb 19, 2010
Messages
48
Programming Experience
Beginner
I am making a project for school with visual basic and its a mcdonalds ordering system. There are 50+ buttons which you can press to remove an item and my code works fine. only problem is it is waaaaayyyy to long and can be shortened with a wildcard. Does anyone know what a good way to put a wildcard in the following code would be:

Private Sub btnsmallfries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsmallfries.Click
If Label2.Text = "Buy Mode" Then
lstorders.Items.Remove("Small Fries $1.95 x1")
lstorders.Items.Remove("Small Fries $1.95 x2")
lstorders.Items.Remove("Small Fries $1.95 x3")
lstorders.Items.Remove("Small Fries $1.95 x4")
lstorders.Items.Remove("Small Fries $1.95 x5")
lstorders.Items.Remove("Small Fries $1.95 x6")
lstorders.Items.Remove("Small Fries $1.95 x7")
lstorders.Items.Remove("Small Fries $1.95 x8")
lstorders.Items.Remove("Small Fries $1.95 x9")
lstorders.Items.Remove("Small Fries $1.95 x10")

totalprice = totalprice + 3.25
txtcost.Text = ("$" & totalprice & "")
smallfriescount = smallfriescount + 1
lstorders.Items.Add("Small Fries $1.95 x" & smallfriescount & "")
End If
If Label2.Text = "Sell Mode" Then

lstorders.Items.Remove("Small Fries $1.95 x1")
lstorders.Items.Remove("Small Fries $1.95 x2")
lstorders.Items.Remove("Small Fries $1.95 x3")
lstorders.Items.Remove("Small Fries $1.95 x4")
lstorders.Items.Remove("Small Fries $1.95 x5")
lstorders.Items.Remove("Small Fries $1.95 x6")
lstorders.Items.Remove("Small Fries $1.95 x7")
lstorders.Items.Remove("Small Fries $1.95 x8")
lstorders.Items.Remove("Small Fries $1.95 x9")
lstorders.Items.Remove("Small Fries $1.95 x10")

totalprice = totalprice - 3.25
txtcost.Text = ("$" & totalprice & "")
smallfriescount = smallfriescount - 1
If smallfriescount = 0 Then
Else
lstorders.Items.Add("Small Fries $1.95 x" & smallfriescount & "")

End If
End If
End Sub
 
Quickly looking at the code (try to use the Code tags makes it easier to read code) I am not sure what the point of all the
VB.NET:
 lstorders.Items.Remove()
are for when you can use a simple
VB.NET:
 lstorders.Items.Clear()
 
I'm not exactly sure what you are doing...

Do you have a big list of items in lstorders, and when the btnsmallfries.Click event runs you are to remove all items in lstOrders that pertains to small fries?

If so you can use a loop and find items that contain 'Small Fries'.
VB.NET:
For Each Item In lstOrders
   If <currentItem> Contains "Small Fries" Then
       Remove Item From List
   End If
Next
 
well basically its adding a listbox item with a count of how many items there are in the listbox after and it deletes the old verison of the item...

So if there is an item "small fries x4" in the listbox it will be replaced with "small fries x 5"

But this only works up to 10 and i need to to contriune on forever and i need ot code to just have 1-3 liines since i cant have this for every button (which i do)
 
I know this a spoiler.

Here ya go. There is one button example, you can setup each of the buttons the same way.


VB.NET:
Private Sub btnSmallFries_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSmallFries.Click
        ProcessOrders("Small Fries", 1.95D, iSmallFries)
    End Sub

    Private Sub ProcessOrders(ByVal strItemOrdered As String, ByVal decItemAmount As Decimal, ByRef intItemCount As Integer)

        'i used a checkbox to tell what mode the form is in, you would check for 
        ' your label text
        If chkIsBuyMode.Checked Then
            intItemCount += 1
            TotalOrders += decItemAmount

        Else
            If intItemCount > 0 Then
                intItemCount -= 1
                TotalOrders -= decItemAmount
            End If
        End If

        'determine which one to remove
        For Each obj As Object In lstOrders.Items
            If obj.ToString().Contains(strItemOrdered) Then
                'removes the object
                lstOrders.Items.Remove(obj)
                Exit For
            End If
        Next

        'now add the item with the correct amounts back in if need be
        If intItemCount > 0 Then
            lstOrders.Items.Add(String.Format("{0}  {1:c} x {2} = {3:c}", strItemOrdered, decItemAmount, intItemCount, (decItemAmount * intItemCount)))
        End If

        lblTotalOrder.Text = String.Format("{0:c}", TotalOrders)

        lstOrders.Refresh()


    End Sub
 
Thanks so much!!!

It worked very well.

I now just have to change each button but now when i am adding more buttons it is going to be much more simple.

The code you have used works globally for all buttons.

I did have to change a few names from your code but it worked fantastically none the less.

Thanks for all your help :)

From philip
 
Back
Top