Join 300,452 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!
I am trying to create a basic VB.Net program that allows the user to input the number of pieces of pizza ordered, number of fries ordered and the number of soft drinks ordered that gives a bill total in a text box and displays the bill in a list view box. The program runs put does not provide output, any help would be appreciated, thanks.
CODE
Public Class Bill
Private Sub Bill_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'variables used to calculate restaurant bill Dim P As Double Dim F As Double Dim D As Double
'Restaurant Bill Calculation Formula Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25)
'Allows Restaurant Bill to be displayed Me.Total.Text = Bill.ToString
'Prepare BillView With BillView .View = View.Details .Columns.Add("Item", 100, HorizontalAlignment.Right) .Columns.Add("Quantity", 100, HorizontalAlignment.Right) .Columns.Add("Price", 100, HorizontalAlignment.Right) End With
End Sub
Private Sub Pizza_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pizza.TextChanged
'text box for user to input number of pizzas
End Sub
Private Sub Fries_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fries.TextChanged
'text box for user to input number of french fries
End Sub
Private Sub Drinks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drinks.TextChanged
'text box for user to input number of soft drinks
End Sub
Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
'Close Restarurant Bill Program Me.Close()
End Sub
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
'Clears data for new bill Pizza.Clear() Fries.Clear() Drinks.Clear() BillView.Items.Clear() End Sub
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
'Variables Dim P As Double Dim F As Double Dim D As Double Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25) Dim Total = 0
'Convert to double If Not Double.TryParse(Pizza.Text, P) Then MsgBox("Please enter a valid number of pieces of pizza.") Exit Sub End If
If Not Double.TryParse(Fries.Text, F) Then MsgBox("Please enter a valid number of orders of fries.") Exit Sub End If
If Not Double.TryParse(Drinks.Text, D) Then MsgBox("Please enter a valid number of drinks.") Exit Sub End If
'Clear List View With BillView .BeginUpdate() .Items.Clear() End With
'Calculate Bill Dim newItem As ListViewItem Dim Item As Double Dim Quantity As Double Dim Price As Double
'Initiates Bill total Me.Total.Text = Bill.ToString
Total = (P * 1.75 + F * 2 + D * 1.25)
'Write output to form newItem = New ListViewItem With newItem .Text = Bill.ToString() .SubItems.Add(Item.ToString("C")) .SubItems.Add(Quantity.ToString("C")) .SubItems.Add(Price.ToString("C")) End With
'Update List View With BillView .EndUpdate() End With
End Sub
Private Sub BillView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BillView.SelectedIndexChanged
'Pizza Bill output
End Sub
Private Sub Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Total.TextChanged
I haven't really worked with listviews a lot, but there's something thats bothering me.
CODE
Dim newItem As ListViewItem Dim Item As Double Dim Quantity As Double Dim Price As Double
'Initiates Bill total Me.Total.Text = Bill.ToString
Total = (P * 1.75 + F * 2 + D * 1.25)
'Write output to form newItem = New ListViewItem With newItem .Text = Bill.ToString() .SubItems.Add(Item.ToString("C")) .SubItems.Add(Quantity.ToString("C")) .SubItems.Add(Price.ToString("C")) End With
'Update List View With BillView .EndUpdate() End With
It's here, aren't you supposed to somehow, you know, add the newItem into the Items of the listview? The way I see it, is that the code just adds the things into a variable and then doesnät do anything after that.
HEY, wait a sec, theres an even bigger problem there.
CODE
Total = (P * 1.75 + F * 2 + D * 1.25)
The way I see this, is that your trying to cast a Double into a textbox. Isn't VS supposed to give an error with something like this? Were you trying to do this:
CODE
Total.Text = CStr(P * 1.75 + F * 2 + D * 1.25)
EDIT: Okay, tested, I was right. You need this after you set the listitems properties:
CODE
BillView.Items.Add(newItem)
This post has been edited by juunas: 1 Jul, 2009 - 08:02 AM
Thanks but the program still needs work. The program now does show a zero balance on the bill view but no total in the total text box. Any other suggestions but the calculate button does work but gives a zero output. the code is below, thanks.
[code]
Public Class Bill
Private Sub Bill_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'variables used to calculate restaurant bill Dim P As Double Dim F As Double Dim D As Double
'Restaurant Bill Calculation Formula Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25)
'Allows Restaurant Bill to be displayed Me.Total.Text = Bill.ToString
'Prepare BillView With BillView .View = View.Details .Columns.Add("Item", 100, HorizontalAlignment.Right) .Columns.Add("Quantity", 100, HorizontalAlignment.Right) .Columns.Add("Price", 100, HorizontalAlignment.Right) End With
End Sub
Private Sub Pizza_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Pizza.TextChanged
'text box for user to input number of pizzas
End Sub
Private Sub Fries_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fries.TextChanged
'text box for user to input number of french fries
End Sub
Private Sub Drinks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Drinks.TextChanged
'text box for user to input number of soft drinks
End Sub
Private Sub ExitBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitBtn.Click
'Close Restarurant Bill Program Me.Close()
End Sub
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
'Clears data for new bill Pizza.Clear() Fries.Clear() Drinks.Clear() BillView.Items.Clear() End Sub
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
'Variables Dim P As Double Dim F As Double Dim D As Double Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25) Dim T = Total.Text
'Convert to double If Not Double.TryParse(Pizza.Text, P) Then MsgBox("Please enter a valid number of pieces of pizza.") Exit Sub End If
If Not Double.TryParse(Fries.Text, F) Then MsgBox("Please enter a valid number of orders of fries.") Exit Sub End If
If Not Double.TryParse(Drinks.Text, D) Then MsgBox("Please enter a valid number of drinks.") Exit Sub End If
'Clear List View With BillView .BeginUpdate() .Items.Clear() End With
'Calculate Bill Dim newItem As ListViewItem Dim Item As Double Dim Quantity As Double Dim Price As Double
'Initiates Bill total Me.Total.Text = Bill.ToString Total.Text = CStr(P * 1.75 + F * 2 + D * 1.25)
'Write output to form newItem = New ListViewItem With newItem .Text = Bill.ToString() .SubItems.Add(Item.ToString("C")) .SubItems.Add(Quantity.ToString("C")) .SubItems.Add(Price.ToString("C")) BillView.Items.Add(newItem) End With
'Update List View With BillView .EndUpdate() End With
End Sub
Private Sub BillView_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BillView.SelectedIndexChanged
'Pizza Bill output
End Sub
Private Sub Total_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Total.TextChanged
Yeah, I know whats missing. You're not inputing the values from the textboxes to the variables P, F and D. I suppose those are the variables that list the amounts?
So, here's the altered Calculate button's Click event:
CODE
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
Dim P As Double Dim F As Double Dim D As Double Dim Bill As Double Dim T = Total.Text
If Not Double.TryParse(Pizza.Text, P) Then MsgBox("Please enter a valid number of pieces of pizza.") Else P = CDbl(Pizza.Text) Exit Sub End If
If Not Double.TryParse(Fries.Text, F) Then MsgBox("Please enter a valid number of orders of fries.") Else F = CDbl(Fries.Text) Exit Sub End If
If Not Double.TryParse(Drinks.Text, D) Then MsgBox("Please enter a valid number of drinks.") Else D = CDbl(Drinks.Text) Exit Sub End If
Bill = (P * 1.75 + F * 2 + D * 1.25)
With BillView .BeginUpdate() .Items.Clear() End With
Dim newItem As ListViewItem Dim Item As Double Dim Quantity As Double Dim Price As Double
Me.Total.Text = Bill.ToString Total.Text = CStr(P * 1.75 + F * 2 + D * 1.25)
newItem = New ListViewItem With newItem .Text = Bill.ToString() .SubItems.Add(Item.ToString("C")) .SubItems.Add(Quantity.ToString("C")) .SubItems.Add(Price.ToString("C")) End With
BillView.Items.Add(newItem)
With BillView .EndUpdate() End With
End Sub
(I removed the comments because this forum doesn't support them well and colors code funnily with VB .NET comments)
If my post was helpful, please click the "This post was helpful" link under my signature.
EDIT: Edited the code a bit. Oh, and one more thing what are the Item, Quantity and Price variables and why on earth is the Item variable a double? If you tell me what they are supposed to do, I can help you better.
EDIT2: Okay, I think I know what everything is supposed to do now. Give me like 15 minutes and I'll make the entire code.
This post has been edited by juunas: 1 Jul, 2009 - 11:34 AM
Okay, everything done. I'm just sending a new message instead of editing so you would get a notification.
Here's the entire code, tested and works. Also, I used the exact same names for textboxes and the listview, so you can copy and paste it directly.
CODE
Public Class Bill
Private Sub Bill_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'variables used to calculate restaurant bill Dim P As Double Dim F As Double Dim D As Double
'Restaurant Bill Calculation Formula Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25)
'Allows Restaurant Bill to be displayed Me.Total.Text = Bill.ToString
'Prepare BillView With billView .View = View.Details .Columns.Add("Item", 100, HorizontalAlignment.Right) .Columns.Add("Quantity", 100, HorizontalAlignment.Right) .Columns.Add("Price", 100, HorizontalAlignment.Right) End With End Sub
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click 'Clears data for new bill Pizza.Clear() Fries.Clear() Drinks.Clear() billView.Items.Clear() Total.Clear() End Sub
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
Dim P As Double Dim F As Double Dim D As Double Dim Bill As Double Dim T = Total.Text Dim inputWrongFlagP As Boolean = False Dim inputWrongFlagF As Boolean = False Dim inputWrongFlagD As Boolean = False Double.TryParse(Pizza.Text, P) Double.TryParse(Fries.Text, F) Double.TryParse(Drinks.Text, D)
If P = 0 Then If Pizza.Text = "" Then inputWrongFlagP = False ElseIf P = 0 Then MsgBox("Please enter a valid number of pieces of pizza.") inputWrongFlagP = True End If End If If F = 0 Then If Fries.Text = "" Then inputWrongFlagF = False ElseIf F = 0 Then MsgBox("Please enter a valid number of pieces of fries.") inputWrongFlagF = True End If End If If D = 0 Then If Drinks.Text = "" Then inputWrongFlagD = False ElseIf D = 0 Then MsgBox("Please enter a valid number of pieces of drinks.") inputWrongFlagD = True End If End If
If inputWrongFlagP = False And inputWrongFlagF = False And inputWrongFlagD = False Then Bill = (P * 1.75 + F * 2 + D * 1.25)
With billView .BeginUpdate() .Items.Clear() End With
Dim newItem As ListViewItem Dim Item As String Dim Quantity As Integer Dim Price As Double
If P > 0 Then Item = "Pizza" Quantity = CInt(P) Price = Quantity * 1.75
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With
billView.Items.Add(newItem) End If
If F > 0 Then Item = "Fries" Quantity = CInt(F) Price = Quantity * 2
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With
billView.Items.Add(newItem) End If
If D > 0 Then Item = "Drinks" Quantity = CInt(D) Price = Quantity * 1.25
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With
billView.Items.Add(newItem) End If
Total.Text = Bill.ToString
With billView .EndUpdate() End With End If
End Sub End Class
You are welcome
This post has been edited by juunas: 1 Jul, 2009 - 11:53 AM
Thank you very much. I compared your code to my code and now I understand what I was doing wrong.
QUOTE(juunas @ 1 Jul, 2009 - 11:51 AM)
Okay, everything done. I'm just sending a new message instead of editing so you would get a notification.
Here's the entire code, tested and works. Also, I used the exact same names for textboxes and the listview, so you can copy and paste it directly.
CODE
Public Class Bill
Private Sub Bill_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'variables used to calculate restaurant bill Dim P As Double Dim F As Double Dim D As Double
'Restaurant Bill Calculation Formula Dim Bill As Double = (P * 1.75 + F * 2 + D * 1.25)
'Allows Restaurant Bill to be displayed Me.Total.Text = Bill.ToString
'Prepare BillView With billView .View = View.Details .Columns.Add("Item", 100, HorizontalAlignment.Right) .Columns.Add("Quantity", 100, HorizontalAlignment.Right) .Columns.Add("Price", 100, HorizontalAlignment.Right) End With End Sub
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click 'Clears data for new bill Pizza.Clear() Fries.Clear() Drinks.Clear() billView.Items.Clear() Total.Clear() End Sub
Private Sub Calculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
Dim P As Double Dim F As Double Dim D As Double Dim Bill As Double Dim T = Total.Text Dim inputWrongFlagP As Boolean = False Dim inputWrongFlagF As Boolean = False Dim inputWrongFlagD As Boolean = False Double.TryParse(Pizza.Text, P) Double.TryParse(Fries.Text, F) Double.TryParse(Drinks.Text, D)
If P = 0 Then If Pizza.Text = "" Then inputWrongFlagP = False ElseIf P = 0 Then MsgBox("Please enter a valid number of pieces of pizza.") inputWrongFlagP = True End If End If If F = 0 Then If Fries.Text = "" Then inputWrongFlagF = False ElseIf F = 0 Then MsgBox("Please enter a valid number of pieces of fries.") inputWrongFlagF = True End If End If If D = 0 Then If Drinks.Text = "" Then inputWrongFlagD = False ElseIf D = 0 Then MsgBox("Please enter a valid number of pieces of drinks.") inputWrongFlagD = True End If End If
If inputWrongFlagP = False And inputWrongFlagF = False And inputWrongFlagD = False Then Bill = (P * 1.75 + F * 2 + D * 1.25)
With billView .BeginUpdate() .Items.Clear() End With
Dim newItem As ListViewItem Dim Item As String Dim Quantity As Integer Dim Price As Double
If P > 0 Then Item = "Pizza" Quantity = CInt(P) Price = Quantity * 1.75
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With
billView.Items.Add(newItem) End If
If F > 0 Then Item = "Fries" Quantity = CInt(F) Price = Quantity * 2
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With
billView.Items.Add(newItem) End If
If D > 0 Then Item = "Drinks" Quantity = CInt(D) Price = Quantity * 1.25
newItem = New ListViewItem With newItem .Text = Item .SubItems.Add(Quantity.ToString) .SubItems.Add(Price.ToString("C")) End With