Visual Basic Program

Associate
Joined
24 Jun 2008
Posts
1,168
You 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.

You could set up a proper object structure and use that, but this looks like GCSE programming so just go with a couple of functions or a sub.

here is the first few lines to start you off. remember decimals rather than integers for the money.
Code:
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
 
Last edited:
Associate
OP
Joined
17 Nov 2011
Posts
852
What help are you actually asking for?
I'm completely stuck and if possible for someone to complete the code of the program for me

You 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.
Thanks for the suggestion, I'll edit the layout
 
Associate
Joined
12 Jun 2007
Posts
121
this might help. As Simon mentioned, you probably want to use a button to run the calculations rather than doing it on TextChanged events.

Code:
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
 
Associate
Joined
24 Jun 2008
Posts
1,168
It sounded like a school assignment so I wasn't going to give him the code. At least make him work a little and learn something.
The code isn't exactly difficult and he had it written in psudocode at the start.

oh and I don't think .toLower works with old VB!
 
Associate
Joined
24 Jun 2008
Posts
1,168
Yes, lblCost txtNumberOfLunches txtDelCode etc.

Then it makes more sense when you read/write the code behind.

I don't think you need the two cost boxes. You have to ask yourself what will you be displaying in them and do you need it?

Remember to validate what the user enters or they could write words in the numbers box and numbers in the code box. Maybe have a dropdown box for the deliveryCode rather than a free text box.
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
I've added a Combo Box with the list,

BFi8o.jpg


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

Combination of the psudocode you already have (you've already mentioned what you need the code to do and how in the above question), Combobox '_Click' event and Combobox 'Text' or 'List' (+index) property will help you sort this issue.

Failing that, look on MSDN and Google (there's hundreds of examples out there if you do a five second search).
 
Associate
Joined
24 Jun 2008
Posts
1,168
Your list should be:
Delivery code A
Delivery code B
Delivery code C
Delivery code D
whatever.

The more than 10 and less than 10 lunches is determined by the number they put in the other text box.

Code:
    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
 
Last edited:
Associate
Joined
24 Jun 2008
Posts
1,168
So there's no need for less/more than 10 lunches, it will determined by the Number of Lunches textbox
Correct.


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
Working step by step, what will be the code of the Number of Lunches and Cost textbox?

7iHaX.jpg
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.
If you had said that "the cost is just £3 each unless it's over 10 lunches then it is £2.50 each". That would be fine, on the textchanged event just have
If number of lunches > = 10 then
lunch cost = number of lunches * 2.5
else
lunch cost = number of lunches * 3

Because you are adding in the delivery cost, it makes no sense to have the lunch cost box at all as it would be the total cost box.

I've added a Collect column which has either Yes or No,

5VK3g.jpg


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
Why not just have "Collect" in the delivery type dropdown box? Which I assume is what the "C" already stands for?

I've changed the Delivery column layout to this,

6w5l9.jpg


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
Fair enough. I still say "C" is collection though.

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

No problem. I don't mind helping, but I'm not giving you the whole thing until you've at least had a stab at it!
 
Last edited:
Associate
Joined
14 May 2010
Posts
1,136
Location
Somerset
Just want to quickly add this. If the user should be able to pick just one option from multiple options (such as the 'Yes' or 'No' and the 'C' or 'Other' above) then you should be using Radio buttons (also called option buttons) rather than checkboxes.

Checkboxes should be used when the user is allowed to tick more than one option at once.
 
Associate
Joined
24 Jun 2008
Posts
1,168
You aren't setting the variable NumberOfLunches before trying to use it to work out the lunchCost variable.

so move the first 5 lines down between the
Code:
NumberOfLunches = tctNumberOfLunches.Text
and
Code:
Answer = lunchCost * NumberOfLunches
lines.

I'd still check that the NumberOfLunches variable contains a number and is greater than 0 or you sum wont work.


Two ways to check the field is numeric only.
Using the Validate event of the text box:
Code:
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

or on the keyPress event to stop them actually typing in anything other than numbers:

Code:
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
 
Last edited:
Back
Top Bottom