Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Private Sub btnTotalCost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotalCost.Click

        lblTotalCost.Text = "YOU HAVE ORDERED:" & vbNewLine

        lblTotalCost.Text = lblTotalCost.Text & "ROOM TYPE" & vbNewLine & "--------" & vbNewLine

        If radLuxury.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Luxury = $210" & vbNewLine
        ElseIf radSuperior.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Superior = $150" & vbNewLine
        Else
            lblTotalCost.Text = lblTotalCost.Text & "Standard = $110" & vbNewLine
        End If

        lblTotalCost.Text = lblTotalCost.Text & vbNewLine & "MEAL OPTIONS" & vbNewLine & "--------" & vbNewLine

        If chkBufferBreakfast.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Buffer Breakfast = $40" & vbNewLine
        End If
        If chkBufferDinner.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Buffer Dinner = $60" & vbNewLine
        End If

        lblTotalCost.Text = lblTotalCost.Text & vbNewLine & "AMENITIES" & vbNewLine & "--------" & vbNewLine

        If chkInternet.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Internet = $10" & vbNewLine
        End If
        If chkEntertainment.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Entertainment = $20" & vbNewLine
        End If
        If chkSpaServices.Checked Then
            lblTotalCost.Text = lblTotalCost.Text & "Spa Services = $50" & vbNewLine
        End If



        
    End Sub


What I have tried:

i have tried the code above . it works fine. but it only shows the breakdown of the costs. i want the label to show the total cost as well. what more should i add to the code??
please help
Posted
Updated 10-Sep-20 22:31pm
Comments
Richard MacCutchan 11-Sep-20 4:49am    
This is your fourth posting of this same message. Please post in one place only, and do not abuse the site.

1 solution

Firstly all that string concatenation is messy - have a look at Using the StringBuilder Class in .NET | Microsoft Docs[^] it will be far easier to follow your code and you will only assign to the textbox once

Secondly - The total amount - add it up as you go (not sure why you have used hard-coded amounts) then add a line for that at the end

Edit - something like this (note - this is untested) and there are even better ways of doing this
VB
Dim sb As StringBuilder = New StringBuilder("YOU HAVE ORDERED:" & vbNewLine)
Dim totCost As Double = 0.0

sb.Append "ROOM TYPE" & vbNewLine & "--------" & vbNewLine

If radLuxury.Checked Then
    sb.Append "Luxury = $210" & vbNewLine
    totCost += 210
ElseIf radSuperior.Checked Then
    sb.Append "Superior = $150" & vbNewLine
    totCost += 150
Else
    sb.Append "Standard = $110" & vbNewLine
    totCost += 110
End If

' all your other lines of the bill ...

sb.Append vbNewLine & "Total Cost: " & totCost.ToString()

lblTotalCost.Text = sb.ToString


Also have a look at https://www.dotnetperls.com/format-vbnet[^]
 
Share this answer
 
v3
Comments
Richard MacCutchan 11-Sep-20 4:51am    
See my comment above.
CHill60 11-Sep-20 5:54am    
Good spot - thanks for the heads up
Rikansha kumar 11-Sep-20 4:57am    
Thanks for the help.
However, I'm looking for code that is very basic as I'm a learner. i do not understand very complicated programming.
If possible, could you please advise that is simpler.
CHill60 11-Sep-20 6:02am    
You couldn't get much more basic than this to be honest - it's not complicated.
Don't be frightened of something you haven't seen before.
totCost += 210
is the same as
totCost = totCost + 210
and means "add 210 to whatever you had before in the variable totCost". My way is just a common and neater way of saying the same thing. Similarly
sb.Append "Luxury = $210" & vbNewLine
is essentially the same as
lblTotalCost.Text = lblTotalCost.Text & "Luxury = $210" & vbNewLine
just in a far, far more efficient way.
The key point here is that you need to keep a running total of the cost - I used the variable totCost to do that - and then add it in to the text at the very end of your function.
You can do it the long-winded way, or you can do it properly, it's up to you. My advice is to get into good habits as soon as you can
Rikansha kumar 11-Sep-20 15:04pm    
Thank you very much for the detailed explanation. I managed to code it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900