Visual Basic Program

Associate
Joined
24 Jun 2008
Posts
1,168
are vb program "good" for just uni/college projects or can they be used to create real life projects?

Well this is VB.net but both this and "old" VB are widely used all though the business world.

There are a lot of C#.net programs out there as well as VB is seen as a bit second rate.
 
Associate
OP
Joined
17 Nov 2011
Posts
852
It'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

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

vzE.jpg
 
Soldato
Joined
3 Jun 2005
Posts
3,119
Location
The South
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

You want to construct a string with the currency symbol and cost and the set the label text to that; for example -
Code:
Label.text = "currency-symbol" & intCost


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
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.


Unfortunately it appears you're getting Simon to knock up the code for you rather than you doing any reading or learning to attempt to solve the problems yourself (simple Google search would have given you hundreds, if not thousands, of examples on all the issues you've posted).
I really recommend that you go to the MSDN site, as it's full of examples and documentation on controls and the VB language. Similarly i recommend purchasing a decent book on VB.
 
Last edited:
Associate
Joined
24 Jun 2008
Posts
1,168
Most of the code is up in post 6 I'm sure you can work something out from that.

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

Or loads of other ways readily available on basic learn VB.net websites.

But it will implicitly convert it correctly.

You don't appear to have got any further with this since the 3rd or even tried to do any more.
Are actually trying to learn this or do you just want the answer? If so I can send you an exe for you to run.
 
Last edited:
Associate
OP
Joined
17 Nov 2011
Posts
852
You want to construct a string with the currency symbol and cost and the set the label text to that; for example -
Code:
Label.text = "currency-symbol" & intCost
Do I add the string code to the Label or the textbox above?

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
Where do I add the string code?

You don't appear to have got any further with this since the 3rd or even tried to do any more.
I tried to code it but when I run it, it says there are problems

Are actually trying to learn this or do you just want the answer? If so I can send you an exe for you to run.
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 practise

I've sent you the program via Trust
 
Associate
Joined
24 Jun 2008
Posts
1,168
You 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.
 
Last edited:
Associate
OP
Joined
17 Nov 2011
Posts
852
You 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.
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 please

I'll have a look at the tutorials
 
Associate
Joined
24 Jun 2008
Posts
1,168
Here you go:
Quiet from the peanut gallery. I know it could be better. ;)

Code:
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

You will have to rename a couple of your controls as I call labels lbl*** and have done here.
Also you should probably remove the text from the labels as it will look weird.

I have realised what you mean about the label controls vanishing when you blank the text property.
You can still see them/get to them in the IDE. Look on the form. Click on any control and bring up the property page.
at the top there is a dropdown that allows you to access all the controls on the form.
 
Last edited:
Associate
OP
Joined
17 Nov 2011
Posts
852
No comment about the code?
Or even a thanks?
Sorry, I just came back today and looked at the code and trying to merge it into the project but I get this error,

KBwjp.jpg


Will depend on the grade he gets from his teacher :p
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 one

Well that's given me a grin! :)
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 others
 
Associate
Joined
24 Jun 2008
Posts
1,168
You'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.

That private sub you posted shouldn't even be in your code anymore it's been replaced.
 
Associate
OP
Joined
17 Nov 2011
Posts
852
You'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.
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 place

So I was thinking of keeping the current code and use the code you posted and merge it together, I just need the Delivery and Calculate code to complete the program

That private sub you posted shouldn't even be in your code anymore it's been replaced.
How do I alter the private sub and code in the screenshot below to the correct one for Delivery part to be working?

KBwjp.jpg
 
Associate
Joined
24 Jun 2008
Posts
1,168
The code I posted was complete and working as per your specification.

you alter the code in the screen shot to the following 3 lines:
Code:
Private sub ComboBox1_selectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Calculate(sender)
End Sub

Nothing else.

However it will throw errors because the combo box in my Calculate routine is called cbDelCode not ComboBox1
 
Last edited:
Back
Top Bottom