Thursday, July 4, 2013

Code for Food Menu - VB.Net

Situation: A Menu Form with the following controls: Checkbox, Label
Checkbox - For Menu - When an item is checked, its value should show and add up to the label total below.
But Sauce should be checked first in order to enable the the other Checkboxes.


Default Interface: 
  • Open MS Visual Studio.
  • Open a New project in Visual Basic.
  • Locate ToolBox by pressing Ctrl+Alt+X.
  • Double click on Group Box to include it in the form. Move the Group Box to the center of the form.
  • Put 7 Checkboxes inside the Group Box.
  • Set the 7 Checkboxes to the following:
    • By default the name of Checkboxes are Checkbox 1, Checkbox 2 and so on.
    • We will change its property to: (Properties to change: Name Property, Text Property and Enabled Property)
Checkbox 1 - Name: chkHam Text: Ham P25.00 Enabled: False
Checkbox 2 - Name: chkCheese Text: Cheese P10.00 Enabled: False
Checkbox 3 - Name: chkPineapple Text: Pineapple P15.00 Enabled: False
Checkbox 4 - Name: chkOlives Text: Olives P20.00 Enabled: False
Checkbox 5 - Name:chkPepperoni Text: Pepperoni P25.00 Enabled: False
Checkbox 6 - Name: chkBacon Text: Bacon P30.00 Enabled: False
Checkbox 7 - Name: chkSauce Text: Sauce P10.00 Enabled: False
 
  • Put 2 Labels on the form
  • Change its properties to the following:

Label 1 - Name: lbltotal Text: TOTAL
Label 2 - Name: lbltotalamount Text: 0
 


 

Code:


'Code by: Emeliza Ducos
' July 4, 2013

 
Public Class Form1
Dim hamAmt As Integer
Dim cheeseAmt As Integer
Dim pineappleAmt As Integer
Dim olivesAmt As Integer
Dim pepperoniAmt As Integer
Dim baconAmt As Integer
Dim sauceAmt As Integer
Dim total As Integer
 
 
'Double Click chkSauce and enter the following code
Private Sub chkSauce_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkSauce.CheckedChanged
'Exp: If chkSauce is not checked the other 6 checkboxes is disabled. And "Select Sauce First" is displayed on the lbltotalamount.
If chkSauce.CheckState = 0 Then
sauceAmt = 10
total = total - sauceAmt
chkHam.Enabled = False
chkCheese.Enabled = False
chkPineapple.Enabled = False
chkOlives.Enabled = False
chkPepperoni.Enabled = False
chkBacon.Enabled = False
lbltotalamount.Text = "Select Sauce First"
'Exp: If chkSauce is checked the other 6 checkboxes will be enabled. And amount of sauce is displayed on the lbltotalamount.
Else
chkHam.Enabled = True
chkCheese.Enabled = True
chkPineapple.Enabled = True
chkOlives.Enabled = True
chkPepperoni.Enabled = True
chkBacon.Enabled = True
sauceAmt = 10
total = total + sauceAmt
lbltotalamount.Text = total
End If
End Sub
 
'Double Click chkHam and enter the following code
Private Sub chkHam_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkHam.CheckedChanged
'Exp: If chkHam is checked, the amount of Ham PLUS amount of Sauce PLUS other amount of any checkbox that has the value of CheckState = 1 (Checked) is displayed on the lbltotalamount. The amount is collected through the Integer total (total = total + hamAmt)
If chkHam.CheckState = 1 Then
hamAmt = 25
total = total + hamAmt
lbltotalamount.Text = total
'Exp: If we unchecked chkHam, the value of ham is deducted from the Integer total.
Else
hamAmt = 25
total = total - hamAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkCheese and enter the following code
Private Sub chkCheese_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCheese.CheckedChanged
If chkCheese.CheckState = 1 Then
cheeseAmt = 10
total = total + cheeseAmt
lbltotalamount.Text = total
Else
cheeseAmt = 10
total = total - cheeseAmt
lbltotalamount.Text = total
End If
End Sub
 
'Double Click chkPineapple and enter the following code 
Private Sub chkPineapple_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPineapple.CheckedChanged
If chkPineapple.CheckState = 1 Then
pineappleAmt = 15
total = total + pineappleAmt
lbltotalamount.Text = total
Else
pineappleAmt = 15
total = total - pineappleAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkOlives and enter the following code
Private Sub chkOlives_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkOlives.CheckedChanged
If chkOlives.CheckState = 1 Then
olivesAmt = 20
total = total + olivesAmt
lbltotalamount.Text = total
Else
olivesAmt = 20
total = total - olivesAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkPepperoni and enter the following code
Private Sub chkPepperoni_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPepperoni.CheckedChanged
If chkPepperoni.CheckState = 1 Then
pepperoniAmt = 25
total = total + pepperoniAmt
lbltotalamount.Text = total
Else
pepperoniAmt = 25
total = total - pepperoniAmt
lbltotalamount.Text = total
End If
End Sub
 
 'Double Click chkBacon and enter the following code
Private Sub chkBacon_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBacon.CheckedChanged
If chkBacon.CheckState = 1 Then
baconAmt = 30
total = total + baconAmt
lbltotalamount.Text = total
Else
baconAmt = 30
total = total - baconAmt
lbltotalamount.Text = total
End If
End Sub
End Class
 
Tell me what you think?
Ask some questions / give feedback on what to correct / suggestions to simplify or improve the program.  
You can ask for the VB.Net file for this program. Email me @ ducos.emeliza@live.com
 

3 comments:

  1. this is great. but what if you combined the food with drinks. and show the list that you buy?

    ReplyDelete
  2. We can add another groupbox with the lists of drinks. Then we can add a label: Bought Items: where we can add the list of items bought. I would love to show you the code for that this Friday.

    ReplyDelete
  3. hello do you have a example of ordering system?

    ReplyDelete