Private Sub calc()
Dim lunchcost As Decimal = 3
Dim deliverycost As Decimal = 2.5
Dim answer As Decimal
Dim numberOfLunches As Integer
Dim deliverycode As String
If isnumeric(numLunches.text) Then
numberOfLunches = numLunches.text
Else
numberoflunches = 1
End If
deliverycode = delcode.text
.......
End Sub
I'm completely stuck and if possible for someone to complete the code of the program for meWhat help are you actually asking for?
Thanks for the suggestion, I'll edit the layoutYou really need to name your controls properly.
Don't use the TextChanged event or you are going to get into trouble really quickly. Use a button somewhere call it calculate and use that to run the calculations.
Private Sub calculate()
'Create variables
Dim lunchcost As Decimal = 3
Dim numberOfLunch As Decimal
Dim deliverycost As Decimal = 2.5
Dim deliveryCode As String = TextBox1.Text
If IsNumeric(TextBox2.Text) Then 'If value in number of lunches is numeric
numberOfLunch = TextBox2.Text 'Set variable to field value
If numberOfLunch >= 10 Then 'If numberOfLunch variable is greater than or equal to 10
deliverycost = 0 'Set deliverycost variable to 0
lunchcost = 2.5 'Set lunchcost variable to 0
ElseIf deliveryCode.ToLower() = "c" Then 'If deliveryCode field value is set to c
deliverycost = 0 'Set deliverycost variable to 0
End If
TextBox4.Text = (numberOfLunch * lunchcost) + deliverycost 'Total cost
TextBox3.Text = numberOfLunch * (lunchcost) 'Total cost excluding delivery cost
TextBox5.Text = deliverycost 'Delivery cost
Else
MsgBox("Please enter a valid value!") 'Display if invalid or no value entered
End If
End Sub
I've added a Combo Box with the list,
![]()
Can you help me with the code of the Cost textbox, which will display the cost of delivery when I select the option of the list, for example, if I select Delivery code is C then the Cost textbox will display FREE or if I select Less than 10 lunches then it will display £2.50
Private Sub cbDelCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDelCode.SelectedIndexChanged
Dim numlunches As Integer
Dim delcode As String = cbDelCode.SelectedItem
If IsNumeric(txtNumLunches.Text) Then
numlunches = txtNumLunches.Text
Else
numlunches = 1
End If
If delcode = "C" OrElse numlunches >= 10 Then
txtDelCost.Text = "FREE"
Else
txtDelCost.Text = "£2.50"
End If
End Sub
Correct.So there's no need for less/more than 10 lunches, it will determined by the Number of Lunches textbox
You are mixing your outputs here. The box that shows Cost below the number of lunches, isn't just dependent on the number of lunches.Working step by step, what will be the code of the Number of Lunches and Cost textbox?The lunch for one person will cost £3, with a delivery charge of £2.50
If orders for ten people or more, the buffet lunches are £2.50 each and delivery is free
![]()
Why not just have "Collect" in the delivery type dropdown box? Which I assume is what the "C" already stands for?I've added a Collect column which has either Yes or No,
![]()
If user select Yes, then it will automatically hit the Calculate button itself without going to the Delivery column, but if user select No, then will have to continue to the Delivery column
Fair enough. I still say "C" is collection though.I've changed the Delivery column layout to this,
![]()
If user select C, then it will display FREE in the Cost textbox, but if user select Other, then depending on the Number of Lunches textbox, if 10 or more lunches is ordered, it will display FREE in the Cost textbox, but if less than 10 lunches ordered, it will display £2.50
I appreciate with the help you're giving me, by doing this practice project, it will help me improve and see how the Visual Basic code is done
NumberOfLunches = tctNumberOfLunches.Text
Answer = lunchCost * NumberOfLunches
Private Sub Text1_Validate(Cancel As Boolean)
If Not IsNumeric(Text1.Text) Then
MsgBox "Please enter numbers only.", vbInformation
'delete if you want
Text1.Text = ""
Cancel = True
End If
End Sub
Private Sub Text1_KeyPress(index As Integer, KeyAscii As Integer)
'Accepts only numeric input
Select Case KeyAscii
Case vbKey0 To vbKey9 'these are 1 to 9
Case vbKeyBack, vbKeyClear, vbKeyDelete
Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab
Case Else
KeyAscii = 0
Beep
End Select
End Sub