Associate
are vb program "good" for just uni/college projects or can they be used to create real life projects?
are vb program "good" for just uni/college projects or can they be used to create real life projects?
The labels display the costs correctly, but without currency, I want to add £ sign before the number in the Label, but I don't know howIt's just text. Put in what you want.
The labels display the costs correctly, but without currency, I want to add £ sign before the number in the Label, but I don't know how
Label.text = "currency-symbol" & intCost
What you've posted here is already the psudocode for this functionality, so it shouldn't be too hard to convert to VB; especially as you mention one way of accomplishing it - hint, two letters and you use four times.Also if the user select Collect or Delivery Code C then delivery price will be FREE which is 0, but if user select Other and depending on how many lunches is ordered, if less than 10 lunches, it will cost £2.50 delivery but if 10 or more lunches ordered, it will be FREE which is 0, I need help on the coding
You want to construct a string with the currency symbol and cost and the set the label text to that; for example -
Do I add the string code to the Label or the textbox above?Code:Label.text = "currency-symbol" & intCost
Where do I add the string code?Personally I'd have
label.text = "£" & fCost.toString
or
label.text = string.concat("£",fCost.toString)
or
dim sb as New System.Text.stringbuilder
sb.append("£")
sb.append(fCost.toString)
label.text = sb.toString
I tried to code it but when I run it, it says there are problemsYou don't appear to have got any further with this since the 3rd or even tried to do any more.
This is my first practise to code in Visual Basic, I was hoping to get it all coded done so I can see the example for my next practiseAre actually trying to learn this or do you just want the answer? If so I can send you an exe for you to run.
I've sent you the program via Trust
Thanks, I'll alter the code, but is it possible for you to help me finish the coding of Delivery and Calculate part so I can use this program as a example for my next practise pleaseYou might want to read up on basic programming principles. Maybe start with a few Hello World! programs with online code.
To make the text on a label change you reference it and set it's text property to a string value.
in the examples above:
Label1 is the object on the screen that you are trying to affect. Yours is lblMealCost or something.
.text is the property of the object that you are trying to change.
= is assigning the value on the right of it to the property on the left.
"£" is a string value in this case a pound sign.
& is a for concatenation of strings.
fCost is a variable that contains the price that the calculation routing sets so 2.50 etc.
.toString() is a method of the object (in this case a float containing 2.50) which make the number into a string.
so you end up with
Label1.text = "£" & fCost.toString
This replaces whatever line currently set the value of the label you said is displaying the costs. remember to change the label object and the float object to match your code.
I'll have a look at the tutorials
Ok, thank you!Ok, I actually wrote the whole thing at the beginning so I'll comment the code and use your front end and post the code later.
Public Class Form1
Private Sub txtNumLunches_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNumLunches.KeyPress
'Accepts only numeric input
'when you press a button when the cursor is in the txtNumLunches box
'this event fires and this code runs.
'the variables in the brackets at the top of this sub are sent by the form itself.
If (Not (Char.IsDigit(e.KeyChar))) And (Not (Char.IsControl(e.KeyChar))) Then
'as expected, char (an object which is a single character) has functions such as
'IsDigit and IsControl which return true if the character you pass to it (e.KeyChar here)
'is a digit or is a control character.
e.Handled = True
'handled=true tells the program that I have dealt with the key press and don't bother doing
'anything else with it. Normally it would write the character in the text box
Beep()
End If
End Sub
'All the following events call the same calculate subroutine which does all the work.
Private Sub txtNumLunches_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtNumLunches.TextChanged
'From the num lunches text box when the text in it changes
Calculate(sender)
End Sub
Private Sub cbDelCode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbDelCode.SelectedIndexChanged
'From the drop down box for delivery code. this runs when the item that is selected is changed
Calculate(sender)
End Sub
Private Sub BtnCal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCal.Click
'From the button click
Calculate(sender)
End Sub
'This is doing all the work
Private Sub Calculate(ByVal sender As System.Object)
'sender is so that I know what brought me here.
'I could have sent sender.name and just had a string here.
'Private Sub Calculate(ByVal whoCalledMe as string)
'Lots of variables initialised to sensible values!
Dim numlunches As Integer = 0
Dim lunchCost As Decimal = 0
Dim delcost As Decimal = 0
'this is the item that is selected from the drop down
'it may not have been selected yet so there is a check for "nothing" later
Dim delcode As String = cbDelCode.SelectedItem
'It will be numeric or empty, but it's worth checking anyway.
If IsNumeric(txtNumLunches.Text) Then
'Set the variable to the number of lunches the user entered
'the convert is to make sure what we are putting in the variable is an integer.
numlunches = Convert.ToInt32(txtNumLunches.Text)
Else
'if it's not numeric, probably empty then clear the lunch cost label
'you can use lblLunchCost.Text = "" but I like String.Empty
lblLunchCost.Text = String.Empty
End If
'You should be able to see what is happening here.
'If the number of lunches is greater or equal to 10 then
'make the lunchCost variable = the number of lunches times by 2.5
If numlunches >= 10 Then
lunchCost = numlunches * 2.5
Else
lunchCost = numlunches * 3
End If
'This pusts the calculated lunch cost into the lunchCost label.
'complete with a pound sign.
lblLunchCost.Text = "£" & lunchCost.ToString
'Delcode is set at the top to the item in the dropdown list that the user has selected.
'Here we are working out if the delivery cost is free or £2.50.
'If the delivery code hasn't been selected yet then don't do anything.
If delcode = "Collect" OrElse delcode = "Delivery Code C" OrElse numlunches >= 10 Then
lblDeliveryCost.Text = "FREE"
ElseIf Not IsNothing(delcode) Then
lblDeliveryCost.Text = "£2.50"
delcost = 2.5
End If
'This bit updates the totalCost label if the user has clicked on the button
'If you don't care, you can remove all but the line:
' lblTotalCost.Text = "£" & (lunchCost + delcost).ToString
If sender.GetType.Name.Equals("Button") Then
'probably the calculate button as there isn't any other on the form.
'if there was you could use
If CType(sender, Button).Name = "BtnCal" Then
'which would work but you would still need to check the type of the object before
'trying to cast it to a button object or you will get an error.
'This bit is the actual bit that writes the total cost to the screen.
'the bit in the brackets adds lunchcost to delcost before displaying it.
lblTotalCost.Text = "£" & (lunchCost + delcost).ToString
End If
End If
End Sub
End Class
No comment about the code?
Or even a thanks?
Will depend on the grade he gets from his teacher![]()
Sorry, I just came back today and looked at the code and trying to merge it into the project but I get this error,No comment about the code?
Or even a thanks?
This isn't a assignment to be submitted, I want to see the finish example so I can use it and learn from the next oneWill depend on the grade he gets from his teacher![]()
I'm not that kind of a person, I've given you credit on the bottom of the program as sometimes I'll show this program to help othersWell that's given me a grin!![]()
I've copy and pasted the code you posted and replace with my current code, and changed the declares but I get quite a lot of errors, I think some code are in the wrong placeYou've got the error because you might not have understood what I posted.
I posted the entire contents of the form.vb file from class start to class end.
You should replace everything that you have with what I posted.
How do I alter the private sub and code in the screenshot below to the correct one for Delivery part to be working?That private sub you posted shouldn't even be in your code anymore it's been replaced.
Private sub ComboBox1_selectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Calculate(sender)
End Sub