Im trying to make a fast food program on which the user will input the amount that he has, then he will click on what he wants, by using check boxes. There are 7 different check boxes with 7 different amounts of money. I got an idea on what to do (they will check on what they want and the program will add up the total and there is only one of each item), but im confused on how to put the amount on where the check boxes (in the program area and not the text area) and add them together. After that all I need to do is add everything up, with sales tax, and tell the user if he has enough money or not. But what im confused on is how do I get the check boxes to a amount of money and add them up (all the ones hes checked) I just dont know how to code this part of the program, once I get one done all I have to do is the same for each one, and everything showed be easy after that. But how do I put an amount on a check box and add them all up?
Dani AI
Nice progress — already has a working UI, demonstrated a clean array approach, and pointed out storing IDs/prices in the control (Tag) or using data binding. A few practical changes will make the app robust and easier to maintain: use Decimal for money, always recalculate the subtotal from the checked controls (don’t rely on incremental add/subtract), apply tax once to the subtotal (avoid per-item rounding differences), validate user input with Decimal.TryParse, and round to two decimals for display.
A compact pattern (uses Tag to hold each item's price, recalculates on every CheckedChanged, and checks the budget before committing) — set each CheckBox.Tag to the numeric price in the designer or at load:
Private Const TAX_RATE As Decimal = 0.06D
Private Function GetPrice(cb As CheckBox) As Decimal
Dim p As Decimal = 0D
Decimal.TryParse(Convert.ToString(cb.Tag), p)
Return p
End Function
Private Function Subtotal() As Decimal
Dim s As Decimal = 0D
For Each c As Control In Me.Controls
If TypeOf c Is CheckBox AndAlso DirectCast(c, CheckBox).Checked Then
s += GetPrice(DirectCast(c, CheckBox))
End If
Next
Return s
End Function In CheckedChanged compute the tentative total = Subtotal (or Subtotal + price if this is the new check), apply tax once, compare to the parsed budget, and if it exceeds the budget revert the check and show a brief message. UX tips: prefer updating a label for the running total instead of MsgBox spam, pre-validate the budget TextBox with TryParse, and switch to a bound list or a quantity control (NumericUpDown) if you need multiple units later.
Recommended Answers
Jump to Post— codeorder 197See if this helps.
Public Class Form1 '// Prices for the CheckBoxes as Decimal Arrays. Private myCoolCheckBoxPrices() As Decimal = {2, 3.5, 4.35, 1.75, 5, 9.99, 0.75} '// To get the value of the 3rd Array, since Index based, you would use: myCoolCheckBoxPrices(2) Private Sub Button1_Click(ByVal sender …
Jump to Post— CodeWord 6There are a number of ways to do this.
Personally, I wouldn't use checkboxes. What if I want more than one item? But then, I don't know your requirements.
Anyway, on to answering your question:
if you aren't using databinding (I would), you could put the itemID into …
All 5 Replies
See if this helps.
Public Class Form1
'// Prices for the CheckBoxes as Decimal Arrays.
Private myCoolCheckBoxPrices() As Decimal = {2, 3.5, 4.35, 1.75, 5, 9.99, 0.75}
'// To get the value of the 3rd Array, since Index based, you would use: myCoolCheckBoxPrices(2)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myTotalPrice As Decimal = 0 '// Reset Total Price.
'// If CheckBox is Checked, add the Decimal Array to myTotalPrice.
If CheckBox1.Checked Then myTotalPrice += myCoolCheckBoxPrices(0)
If CheckBox2.Checked Then myTotalPrice += myCoolCheckBoxPrices(1)
If CheckBox3.Checked Then myTotalPrice += myCoolCheckBoxPrices(2)
If CheckBox4.Checked Then myTotalPrice += myCoolCheckBoxPrices(3)
If CheckBox5.Checked Then myTotalPrice += myCoolCheckBoxPrices(4)
If CheckBox6.Checked Then myTotalPrice += myCoolCheckBoxPrices(5)
If CheckBox7.Checked Then myTotalPrice += myCoolCheckBoxPrices(6)
MsgBox(myTotalPrice.ToString("c")) '// Display result.
End Sub
End Class There are a number of ways to do this.
Personally, I wouldn't use checkboxes. What if I want more than one item? But then, I don't know your requirements.
Anyway, on to answering your question:
if you aren't using databinding (I would), you could put the itemID into the TAG property of the checkbox.
Then you could simply look up the item by id in your datatable (with prices) and add it to the order. Obviously, if they decide to uncheck the item, you would then subtract it.
Ok this is what I got so far
Public Class Form1
'// Prices for the CheckBoxes as Decimal Arrays.
Private myCoolCheckBoxPrices() As Decimal = {3.99, 2.79, 3.29, 1.39, 1.59, 1.99, 2.56}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Hamsandwich.CheckedChanged
Dim myTotalPrice As Decimal = 0 '// Reset Total Price.
'// If CheckBox is Checked, add the Decimal Array to myTotalPrice.
If Hamsandwich.Checked Then myTotalPrice += myCoolCheckBoxPrices(0)
If hamburger.Checked Then myTotalPrice += myCoolCheckBoxPrices(1)
If lettucewrap.Checked Then myTotalPrice += myCoolCheckBoxPrices(2)
If sodapopsmall.Checked Then myTotalPrice += myCoolCheckBoxPrices(3)
If sodapoplarge.Checked Then myTotalPrice += myCoolCheckBoxPrices(4)
If fries.Checked Then myTotalPrice += myCoolCheckBoxPrices(5)
If sidesalad.Checked Then myTotalPrice += myCoolCheckBoxPrices(6)
MsgBox(myTotalPrice.ToString("c")) '// Display result.
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class Now all I need now to be able to finish my assignment is to figure out how after the user selects an item, the application shall calculate the total amount due and compare this to the budget of the customer (amount they have). If the budget allows it, keep the item checked and display the amount remaining. If the budget does not allow, uncheck the item and display a message box explaining that the user does not have enough money.
also I need to put a 6% sales tax to the end amount (which showed be easy, all I need is the end amount and times it be 1.06) and also atleast one If...Then...Else... statement
I figure the If Then Else statements would be for the part of them if they have enough money or not, but I dont know how to code that part right
See If this helps.
Public Class Form1
'// Prices for the CheckBoxes as Decimal Arrays.
Private myCoolCheckBoxPrices() As Double = {3.99, 2.79, 3.29, 1.39, 1.59, 1.99, 2.56}
Private myCustomerTotalSpendingAllowed As Double = 17.59 '// Total a Customer has set for Spending Limit.
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
Hamsandwich.CheckedChanged, hamburger.CheckedChanged, lettucewrap.CheckedChanged, _
sodapopsmall.CheckedChanged, sodapoplarge.CheckedChanged, fries.CheckedChanged, sidesalad.CheckedChanged
Dim myTotalPrice As Double = 0 '// Reset Total Price.
'// If CheckBox is Checked, add the Decimal Array to myTotalPrice.
If Hamsandwich.Checked Then myTotalPrice += myCoolCheckBoxPrices(0)
If hamburger.Checked Then myTotalPrice += myCoolCheckBoxPrices(1)
If lettucewrap.Checked Then myTotalPrice += myCoolCheckBoxPrices(2)
If sodapopsmall.Checked Then myTotalPrice += myCoolCheckBoxPrices(3)
If sodapoplarge.Checked Then myTotalPrice += myCoolCheckBoxPrices(4)
If fries.Checked Then myTotalPrice += myCoolCheckBoxPrices(5)
If sidesalad.Checked Then myTotalPrice += myCoolCheckBoxPrices(6)
Me.Text = myTotalPrice.ToString("c") '// Display result.
'//----- compare TotalPrice with the price the customer has set for spending limit.
If myTotalPrice > myCustomerTotalSpendingAllowed Then '// if price goes over, alert customer with the amount over the set limit.
MsgBox("Total price has went over your customer's spending limit." _
& vbNewLine & "Amount over the limit: " & (myCustomerTotalSpendingAllowed - myTotalPrice).ToString("c"), MsgBoxStyle.Critical)
End If '-----\\
End Sub
End Class I guess that a penny does make a difference after all.:D
Thanks to codeorder this is the code that I needed, to get everything to work
Public Class Form1
'// Prices for the CheckBoxes as Arrays.
Private myCoolCheckBoxPrices() As Double = {3.99, 2.79, 3.29, 1.39, 1.59, 1.99, 2.56}
Private myCustomerTotalSpendingAllowed As Double = 0 '// Total Price allowed to spend.
Private myTotalPrice As Double = 0 '// Total Price declared here now, not within a Sub.
Private myTax As Double = 0.06 '// Tax used to add to Total Price.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'// check if numeric, then set new value for myCustomerTotalSpendingAllowed.
If IsNumeric(TextBox1.Text) Then myCustomerTotalSpendingAllowed = CDbl(TextBox1.Text)
If TextBox1.Text = "" Then '// if no value, add a "0" and selectAll for quick typing.
TextBox1.Text = "0" : TextBox1.SelectAll()
End If
End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged, CheckBox4.CheckedChanged _
, CheckBox5.CheckedChanged, CheckBox6.CheckedChanged, CheckBox7.CheckedChanged
Dim cb As CheckBox = CType(sender, CheckBox) '// gets the CheckBox sender.
If TextBox1.Text = "" Then '// if Value is empty in allowed spending TextBox.
MsgBox("Please enter a Total Allowed Spending Amount.", MsgBoxStyle.Information) '// prompt user.
TextBox1.Select() '// be nice and preselect. :)
Exit Sub '// skip remaining code in this sub.
End If
If cb Is CheckBox1 Then coolPriceChecker(cb, 0) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox2 Then coolPriceChecker(cb, 1) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox3 Then coolPriceChecker(cb, 2) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox4 Then coolPriceChecker(cb, 3) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox5 Then coolPriceChecker(cb, 4) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox6 Then coolPriceChecker(cb, 5) '// send the current CheckBox and the Array Index to use.
If cb Is CheckBox7 Then coolPriceChecker(cb, 6) '// send the current CheckBox and the Array Index to use.
End Sub
Private Sub coolPriceChecker(ByVal selCoolCheckBox As CheckBox, ByVal selCoolPriceFromArray As Integer)
If selCoolCheckBox.Checked Then
myTotalPrice += myCoolCheckBoxPrices(selCoolPriceFromArray) + (myCoolCheckBoxPrices(selCoolPriceFromArray) * myTax)
If myTotalPrice > myCustomerTotalSpendingAllowed Then '//--- if price goes over, alert.
MsgBox("Total price has went over your customer's spending limit." _
& vbNewLine & "Amount over the limit: " & (myCustomerTotalSpendingAllowed - myTotalPrice).ToString("c"), MsgBoxStyle.Critical)
selCoolCheckBox.Checked = False '// Uncheck the CheckBox.
End If '-----\\
Else
myTotalPrice -= myCoolCheckBoxPrices(selCoolPriceFromArray) + (myCoolCheckBoxPrices(selCoolPriceFromArray) * myTax)
End If
Me.Text = myTotalPrice.ToString("c") '// Display result.
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0" '// add allowed price to the TextBox.
End Sub
End Class We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.