Performing Calculations with Modules in Visual Basic - vb.net

I am writing a program for my Visual Basic class that is supposed to be able to calculate the final total price for items selected from one list box and added to another. 2 of the items have sales tax that must be added into the final price. The program also has a Module that is supposed to be used to keep record of all taxes and is used to do all tax-related functions.
Here is the most current code.
Option Strict On
Public Class Form1
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
If (txtQuantity.Text = "") Then
MessageBox.Show("Please enter a quantity for the item you selected")
ElseIf Not Integer.TryParse(txtQuantity.Text, CInt(txtQuantity.Text)) Then
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
Exit Sub
Else
lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
Dim int As Integer
Dim total As Double = 0
For i As Integer = 0 To lstPurchased.Items.Count - 1
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
Integer.TryParse(lst(0), int)
total += TaxesModule.SalesTax(int, lst(1))
Next
MessageBox.Show(CStr(FormatCurrency(total)))
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
lstPurchased.Items.Clear()
txtQuantity.Clear()
End Sub
End Class
And the most recent code for the module
Option Strict On
Module TaxesModule
Private Const DONUT_TAX As Decimal = CDec(0.083)
Private Const RAISIN_TAX As Decimal = CDec(0.02)
Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this
Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
Dim TotalWithSalesTax As Double
If item = "Wheat Bread" Then
TotalWithSalesTax += (Quantity * 1.15)
ElseIf item = "White Bread" Then
TotalWithSalesTax += (Quantity * 1.05)
ElseIf item = "Donuts" Then
TotalWithSalesTax += (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
ElseIf item = "Raisins" Then
TotalWithSalesTax += (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
End If
Return TotalWithSalesTax
End Function
End Module
As the code is written now, the only problem I'm having is that the TotalWithSalesTax for "Raisins" is not calculating correctly. For example, if I select "Raisins" from the list box and add it with a quantity of 1 to the other list box, the total that is displayed in the message box is $0.00.
I'm starting to think the issue is with the following section of code:
For i As Integer = 0 To lstPurchased.Items.Count - 1
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
Because I have tried making changes such as
For i As Integer = 1 ...
And that caused both Donuts and Raisins to give me a total of $0.00 in the message box. So I'm wondering if maybe how the count is set up is not allowing it to go through the entire list, but I don't know how to fix that?
I'm still really new to Visual Basic, and programming in general really and this is my first time working with modules. Can anybody help me figure out what I'm doing wrong, so I can move onto other parts of the program?

I have modified your code, hope this is what you want. Added comment in the code. Note i have removed all Public variables.
Public Class Form1
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click
If (txtQuantity.Text = "") Then
MessageBox.Show("Please enter a quantity for the item you selected")
ElseIf Not Integer.TryParse(txtQuantity.Text, txtQuantity.Text) Then
MessageBox.Show("The quantity entered is not numeric. Please add a numeric quantity.")
Exit Sub
Else
lstPurchased.Items.Add(txtQuantity.Text & " " & lstSale.Text)
End If
End Sub
Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnCalculate.Click
Dim int As Integer
Dim total As Double = 0
'you have to loop through the list to calculate the total
' hope this is what you want
For i As Integer = 0 To lstPurchased.Items.Count - 1
' We split the list item into the quantity and the item name
Dim lst() As String = lstPurchased.Items(i).ToString.Split({CChar(" ")}, 2)
'lst(1) contains the item name
Integer.TryParse(lst(0), int)
total += TaxesModule.SalesTax(int, lst(1))
Next
MessageBox.Show(CStr(FormatCurrency(total)))
End Sub
Private Sub btnClear_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnClear.Click
lstPurchased.Items.Clear()
txtQuantity.Clear()
End Sub
End Class
The module
Module TaxesModule
Private Const DONUT_TAX As Decimal = CDec(0.083)
Private Const RAISIN_TAX As Decimal = CDec(0.02)
Private Const SS_TAX As Decimal = CDec(0.062) ' <-- you are not using this
Public Function SalesTax(ByVal Quantity As Integer, ByVal item As String) As Double
Dim TotalWithSalesTax As Double
If item = "Wheat Bread" Then
TotalWithSalesTax = (Quantity * 1.15)
ElseIf item = "White Bread" Then
TotalWithSalesTax = (Quantity * 1.05)
ElseIf item = "Donuts" Then
TotalWithSalesTax = (Quantity * (0.5 * DONUT_TAX) + (Quantity * 0.5))
ElseIf item = "Raisins" Then
TotalWithSalesTax = (Quantity * (0.25 * RAISIN_TAX) + (Quantity * 0.25))
End If
Return TotalWithSalesTax
End Function
End Module

Related

Updating the current datagridview instead of adding another in VB.net

Hello sorry for asking too many questions im just really a beginner on VB.net but what I wanted to do know is instead of adding another rows how can I update that instead? like Row = Bulgogi,1,45 and then when I click on the picturebox again it will be Row = Bulgogi,2,90 and not adding Bulgogi, 1 ,45 and Bulgogi, 2 ,90 also the Quantity isnt working just stuck in 1 everytime it clicks but the increment is working fine :/
Private BibimbapQuantity = 0
Private BulgogiQuantity = 0
Private BibimbapPrice As Integer
Private BulgogiPrice As Integer
Private TotalPriceInt As Integer
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity
Me.DataGridView2.Rows.Add("Bibimbap", BibimbapQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
BulgogiPrice = 50 * BulgogiQuantity
Dim Satisfy As Integer = 0
Me.DataGridView2.Rows.Add("Bulgogi", BulgogiQuantity, BibimbapPrice)
totalPrice.Text = BibimbapPrice + BulgogiPrice
End Sub
Try to add this before the Me.DataGridView2.Rows.Add
Me.DataGridView2.Rows.Clear()
See the comments in the AddItemToCart() method for explanations.
Public Class Form1
Dim bibimbapQuantity, bulgogiQuantity, kimchiQuantity As Integer
Dim bibimbapTotalPrice, bulgogiTotalPrice, kimchiTotalPrice As Integer
Const bibimbapPrice As Integer = 45, bulgogiPrice As Integer = 50, kimchiPrice As Integer = 35
Private Sub buttonBibimbap_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
bibimbapQuantity += 1
bibimbapTotalPrice = bibimbapPrice * bibimbapQuantity
AddItemToCart("Bibimbap", bibimbapQuantity, bibimbapTotalPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
bulgogiQuantity += 1
bulgogiTotalPrice = bulgogiPrice * bulgogiQuantity
AddItemToCart("Bulgogi", bulgogiQuantity, bulgogiTotalPrice)
End Sub
Private Sub buttonKimchi_Click(sender As Object, e As EventArgs) Handles buttonKimchi.Click
kimchiQuantity += 1
kimchiTotalPrice = kimchiPrice * kimchiQuantity
AddItemToCart("Kimchi", kimchiQuantity, kimchiTotalPrice)
End Sub
Sub AddItemToCart(foodName As String, quantity As Integer, totalPrice As Integer)
Dim foundFood As Boolean = False ' initially the food name is not found yet
For i = 0 To DataGridView1.Rows.Count - 1 ' loop each row in grid
If DataGridView1(0, i).Value = foodName Then ' if current row is the food you're adding
foundFood = True ' food is found
DataGridView1(1, i).Value = quantity ' set new quantity
DataGridView1(2, i).Value = totalPrice ' set new total price
Exit For ' don't check the rest of the rows because it has been found
End If
Next
If Not foundFood Then ' if food is not found in the grid
DataGridView1.Rows.Add(foodName, quantity, totalPrice) ' add a new line in the grid
End If
textboxTotalPrice.Text = bibimbapTotalPrice + bulgogiTotalPrice + kimchiTotalPrice ' calculate total for all foods
End Sub
End Class

remove and reset button for item list

I am trying to create a remove selected item and reset button which removes an item from a list box and reduces the cost of that item in a label. However, The clear button resets the cost and item list text but when clicking a button to add the item again it adds it once into the textbox but brings back the original cost as well as adding the new cost. The Remove selected item button removes the item but not the amount of the cost of that item. What am i doing wrong? Apologies for the bad English.GUI
Option Strict On
Public Class sandwichInterface
Dim DecTotal As Decimal
Private Sub btnCiabatta_Click(sender As Object, e As EventArgs) Handles btnCiabatta.Click
ListBox1.Items.Add("1 Ciabatta £1.50")
ListBox1.Text = ListBox1.Text + ("1 Ciabatta £1.50") + Chr(13)
DecTotal = DecTotal + CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
lblTotal.Text = "Total £" + Format(DecTotal, "###.00")
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
ListBox1.Items.Remove(ListBox1.SelectedItem)
If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
lblTotal.Text = "Total £" + Format(DecTotal, "###.00")
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
ListBox1.Items.Clear()
If DecTotal = lblTotal.Text Then lblTotal.Text = "£0.00"
End Sub
End Class
Urgh, it pains me to even read this code but here goes!
Delete this from the add:
ListBox1.Text = ListBox1.Text + ("1 Ciabatta £1.50") + Chr(13)
Swap these 2 lines round, you can't check the value of the selected item after you removed it.:
ListBox1.Items.Remove(ListBox1.SelectedItem)
If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
By this:
If DecTotal = lblTotal.Text Then lblTotal.Text = "£0.00"
I can only guess you mean
DecTotal = 0
lblTotal.Text = "£0.00"
I also have no idea why you would do this:
If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - CDbl(Microsoft.VisualBasic.Right("1 Ciabatta £1.50", 4))
Instead of:
If ListBox1.SelectedItem.Text = "1 Ciabatta £1.50" Then DecTotal = DecTotal - 1.50
Even if the latter is still pretty awful!
Your whole approach to this is not nice though, passing around money as part of a string and using Microsoft.VisualBasic.Right and Cdbl to convert it to a float is pretty awful practice to say the least.
This code is not meant to paste into your project but to show you how some things could be done. I created a simple class to add objects to the list box. The Button2.Click shows how you could add items to your list box. You would add them one by one probably from Button events. Button 2 also shows how to get a total for the price. The $ before the string means that it is an interpolated string that can include variables. The :c after Total formats Total to local currency.
The button3.click shows how to remove an item from the list and adjust the total.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
ListBox3.Items.Clear()
With ListBox3.Items
.Add(New FoodItem("Ciabatta", 1.5))
.Add(New FoodItem("Good Bread", 0.5))
.Add(New FoodItem("Great Bread", 2.0))
End With
For Each item As FoodItem In ListBox3.Items
Total += item.Price
Next
lblTotal.Text = $"{Total:c}"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim SelectedFoodItem As FoodItem = DirectCast(ListBox3.SelectedItem, FoodItem)
Dim d As Double = SelectedFoodItem.Price
ListBox3.Items.Remove(ListBox3.SelectedItem)
Total -= d
lblTotal.Text = $"{Total:c}"
End Sub
End Class
Public Class FoodItem
Public Sub New()
'Default
End Sub
Public Sub New(objFoodName As String, dblPrice As Double)
FoodName = objFoodName
Price = dblPrice
End Sub
Private _FoodName As String
Public Property FoodName As String
Get
Return _FoodName
End Get
Set(value As String)
_FoodName = value
End Set
End Property
Private _Price As Double
Public Property Price As Double
Get
Return _Price
End Get
Set(value As Double)
_Price = value
End Set
End Property
Public Overrides Function ToString() As String
Return $"{_FoodName} {_Price:c}"
End Function
End Class

Checkbook Program in VB 2010

I am trying to get my program to have the ability to do multiple transactions in a row. Here is my code I have so far:
Public Class checkbook
Dim transAmount As Decimal
Dim newBalance As Decimal
Dim Balance As Decimal
Dim deposit As Decimal
Dim check As Decimal
Dim service As Decimal
Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()
End Sub
Private Sub AboutButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AboutButton.Click
MessageBox.Show("Program: Checkbook Version 1.0 Company: JWEED Description: Updates Balance")
End Sub
Private Sub CalcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CalcButton.Click
Balance = Decimal.Parse(BalanceTextBox.Text)
transAmount = Decimal.Parse(AmountTextBox1.Text)
If DepositRadioButton.Checked Then
deposit = (Balance + transAmount)
Balance = deposit
ElseIf CheckRadioButton.Checked Then
check = Balance - transAmount
If check < 0 Then
check = check - 10
MessageBox.Show("Error: Negavtive Balance")
Balance = check
ElseIf check > 0 Then
Balance = check
End If
ElseIf ServiceRadioButton.Checked Then
service = Balance - (10 + transAmount)
Balance = service
End If
BalanceTextBox.Text = Balance.ToString("C")
AmountTextBox1.Text = transAmount.ToString("C")
End Sub
End Class
It tells me that there is an format issue with my balance after I do the first transaction and try to do a second one.
You have format your balance / amount with currency symbol, as so, please try to change following lines:
Balance = Decimal.Parse(BalanceTextBox.Text, NumberStyles.Currency)
transAmount = Decimal.Parse(AmountTextBox1.Text, NumberStyles.Currency)
Moreover, I would suggest you to init 2 values with Currency too
Reference: Problem parsing currency text to decimal type

How to make an inputbox and text file appear in Visual Basic?

I'm making a program that allows users to see information on songs, play an excerpt of them, and purchase selected ones.
And allow users to click the Purchase button to buy the indicated tune.
When checking out:
Users cannot checkout if they have not purchased any tunes, however they can exit the program.
Use an InputBox so that users can enter their sales tax rate. Since users are entering a value, you must perform data validation on their input.
Allow users to cancel the check out process by clicking the InputBox Cancel button.
When the input box is displayed, the textbox should have the focus, and when an incorrect tax value is added, the incorrect value should be cleared and the textbox should have focus again.
Use Write/Writeline to create a purchase order text file named PurchaseOrder.txt that includes the date the file was created and an itemized list of purchases, the subtotal, tax, and total.
When I click on the "Purchase" button of the selected song and click on the "Check Out" button, I get an error that say: "You have not ordered any items". Please refer to the cmdCheckOut_Click subroutine in the code below. I think that's where I'm getting my errors.
Here's the code:
Public Structure musicInfo
<VBFixedString(30)> Public title As String
<VBFixedString(20)> Public artist As String
<VBFixedString(20)> Public genre As String
<VBFixedString(10)> Public duration As String
Public year As Integer
Public price As Double
<VBFixedString(15)> Public songFileName As String
End Structure
Public Const NoOfTunes = 5
Public songs(NoOfTunes - 1) As musicInfo
Option Explicit On
Imports System.IO
Public Class frmTunes
Public index As Integer
Public purchaseCount As Integer
Public purchasePrice(10) As Decimal
Public purchaseTitle(10) As String
Private Sub frmTunes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
FileOpen(1, "music.dat", OpenMode.Random, , , Len(songs(0)))
For i = 0 To NoOfTunes - 1
FileGet(1, songs(i))
Next
FileClose(1)
cmdPrevious.Visible = False
DisplaySong(0)
End Sub
Sub DisplaySong(ByVal i As Int32)
lblTitle.Text = songs(i).title
lblArtist.Text = songs(i).artist
lblGenre.Text = songs(i).genre
lblDuration.Text = songs(i).duration
lblYear.Text = Convert.ToString(songs(i).year)
lblPrice.Text = Convert.ToString(songs(i).price)
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
My.Computer.Audio.Stop()
End Sub
Private Sub cmdPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPurchase.Click
purchaseTitle(purchaseCount) = lblTitle.Text
purchasePrice(purchaseCount) = Convert.ToDecimal(lblPrice.Text)
purchaseCount = (purchaseCount + 1)
End Sub
Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
index = (index - 1)
If (index < 4) Then
cmdNext.Visible = True
End If
If (index = 0) Then
cmdPrevious.Visible = False
End If
DisplaySong(index)
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
index = (index + 1)
If (index = NoOfTunes - 1) Then
cmdNext.Visible = False
End If
If (index > 0) Then
cmdPrevious.Visible = True
End If
DisplaySong(index)
End Sub
Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
My.Computer.Audio.Play(songs(index).songFileName)
End Sub
Private Sub cmdCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOut.Click
Dim decimal1 As Decimal
Dim decimal3 As Decimal
Dim decimal4 As Decimal
Dim str1 As String = ""
If (Not purchaseCount) Then
MsgBox("You have not ordered any items!", MsgBoxStyle.Exclamation, "Order Error")
Else
Do While ((IsNumeric(str1) Or (Decimal.Compare(decimal3, Decimal.Zero) < 0)) Or (Decimal.Compare(decimal3, (10D)) > 0))
str1 = InputBox("Enter your tax rate as a % between and including 0 - 10:", "Tax Rate", "", -1, -1)
If (str1 <> "") Then
If (Not IsNumeric(str1)) Then
MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
Else
Dim dec3 As Decimal = Convert.ToDecimal(str1)
If ((Decimal.Compare(decimal3, Decimal.Zero) < 0) Or (Decimal.Compare(decimal3, (10D)) > 0)) Then
MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
End If
End If
End If
Loop
Dim StreamWriter As StreamWriter = File.CreateText("PurchaseOrder.txt")
StreamWriter.WriteLine("For Purchases dated: " & DateTime.Now.ToLongDateString())
StreamWriter.WriteLine()
Dim num2 As Integer = (purchaseCount - 1)
Dim num1 As Integer = 0
Do While (num1 <= num2)
StreamWriter.Write(Strings.FormatCurrency(CType(Me.purchasePrice(num1), Decimal) & " "))
StreamWriter.WriteLine(Me.purchaseTitle(num1))
Dim dec1 As Decimal = Decimal.Add(Nothing, Me.purchasePrice(num1))
num1 = (num1 + 1)
Loop
StreamWriter.WriteLine("------")
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal1, Decimal) & " Subtotal"))
Dim decimal2 As Decimal = New Decimal(((Convert.ToDouble(decimal3) * 0.01) * Convert.ToDouble(decimal1)))
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal2, Decimal) & " Tax"))
StreamWriter.WriteLine("------")
Dim dec4 As Decimal = Decimal.Add(decimal1, decimal2)
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal4, Decimal) & " Total"))
MsgBox("Purchase Order has been created", MsgBoxStyle.OkOnly)
StreamWriter.Close()
Me.Close()
End If
End Sub
End Class
Not doesn't do what you think it does. In VB.Net the Not operator performs a bitwise invert on non-boolean value. So if purchaseCount = 1 then Not purchaseCount = 0xFFFFFFFE = -2, which with convert to True. Only an integer value of 0, whould convert to false.
Change the test If (Not purchaseCount) to If (purchaseCount = 0)

My program keeps looping an inputbox. How do I get out of it?

I'm making a program that allows users to see information on songs, play an excerpt of them, and purchase selected ones.
And allow users to click the Purchase button to buy the indicated tune.
When checking out:
Users cannot checkout if they have not purchased any tunes, however they can exit the program.
Use an InputBox so that users can enter their sales tax rate. Since users are entering a value, you must perform data validation on their input.
Allow users to cancel the check out process by clicking the InputBox Cancel button.
When the input box is displayed, the textbox should have the focus, and when an incorrect tax value is added, the incorrect value should be cleared and the textbox should have focus again.
Use Write/Writeline to create a purchase order text file named PurchaseOrder.txt that includes the date the file was created and an itemized list of purchases, the subtotal, tax, and total.
When I click on the "Purchase" button of the selected song and click on the "Check Out" button, I get an inputbox, type the numeric value, however, it continues to loop over and over again. I don't get why this is happening. Please refer to the cmdCheckOut_Click subroutine in the code below. I think that's where I'm getting my error.
Here's the code:
Public Structure musicInfo
<VBFixedString(30)> Public title As String
<VBFixedString(20)> Public artist As String
<VBFixedString(20)> Public genre As String
<VBFixedString(10)> Public duration As String
Public year As Integer
Public price As Double
<VBFixedString(15)> Public songFileName As String
End Structure
Public Const NoOfTunes = 5
Public songs(NoOfTunes - 1) As musicInfo
Option Explicit On
Imports System.IO
Public Class frmTunes
Public index As Integer
Public purchaseCount As Integer
Public purchasePrice(10) As Decimal
Public purchaseTitle(10) As String
Dim decimal1 As Decimal
Dim decimal3 As Decimal
Dim decimal4 As Decimal
Private Sub frmTunes_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
FileOpen(1, "music.dat", OpenMode.Random, , , Len(songs(0)))
For i = 0 To NoOfTunes - 1
FileGet(1, songs(i))
Next
FileClose(1)
cmdPrevious.Visible = False
DisplaySong(0)
End Sub
Sub DisplaySong(ByVal i As Int32)
lblTitle.Text = songs(i).title
lblArtist.Text = songs(i).artist
lblGenre.Text = songs(i).genre
lblDuration.Text = songs(i).duration
lblYear.Text = Convert.ToString(songs(i).year)
lblPrice.Text = Convert.ToString(songs(i).price)
End Sub
Private Sub cmdStop_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
My.Computer.Audio.Stop()
End Sub
Private Sub cmdPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPurchase.Click
purchaseTitle(purchaseCount) = lblTitle.Text
purchasePrice(purchaseCount) = Convert.ToDecimal(lblPrice.Text)
purchaseCount = (purchaseCount + 1)
End Sub
Private Sub cmdPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrevious.Click
index = (index - 1)
If (index < 4) Then
cmdNext.Visible = True
End If
If (index = 0) Then
cmdPrevious.Visible = False
End If
DisplaySong(index)
End Sub
Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click
index = (index + 1)
If (index = NoOfTunes - 1) Then
cmdNext.Visible = False
End If
If (index > 0) Then
cmdPrevious.Visible = True
End If
DisplaySong(index)
End Sub
Private Sub cmdPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlay.Click
My.Computer.Audio.Play(songs(index).songFileName)
End Sub
Private Sub cmdCheckOut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCheckOut.Click
Dim str1 As String = ""
If (purchaseCount = 0) Then
MsgBox("You have not ordered any items!", MsgBoxStyle.Exclamation, "Order Error")
Else
Do Until ((IsNumeric(str1) And (Decimal.Compare(decimal3, Decimal.Zero) <= 10)) And (Decimal.Compare(decimal3, (10D)) >= 0))
str1 = InputBox("Enter your tax rate as a %" & vbCrLf & "between and including 0 - 10:", "Tax Rate", "", -1, -1)
If (str1 <> "") Then
If (Not IsNumeric(str1)) Then
MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
Else
Dim decimal3 As Decimal = Convert.ToDecimal(str1)
If ((Decimal.Compare(decimal3, Decimal.Zero) < 0) Or (Decimal.Compare(decimal3, (10D)) > 0)) Then
MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
End If
End If
End If
Loop
Dim StreamWriter As StreamWriter = File.CreateText("PurchaseOrder.txt")
StreamWriter.WriteLine("For Purchases dated: " & DateTime.Now.ToLongDateString())
StreamWriter.WriteLine()
Dim num2 As Integer = (purchaseCount - 1)
Dim num1 As Integer = 0
Do While (num1 <= num2)
StreamWriter.Write(Strings.FormatCurrency(CType(Me.purchasePrice(num1), Decimal) & " "))
StreamWriter.WriteLine(purchaseTitle(num1))
Dim decimal1 As Decimal = Decimal.Add(Nothing, purchasePrice(num1))
num1 = (num1 + 1)
Loop
StreamWriter.WriteLine("------")
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal1, Decimal)) & " Subtotal")
Dim decimal2 As Decimal = New Decimal(((Convert.ToDouble(decimal3) * 0.01) * Convert.ToDouble(decimal1)))
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal2, Decimal)) & " Tax")
StreamWriter.WriteLine("------")
Dim decimal4 As Decimal = Decimal.Add(decimal1, decimal2)
StreamWriter.WriteLine(Strings.FormatCurrency(CType(decimal4, Decimal)) & " Total")
MsgBox("Purchase Order has been created", MsgBoxStyle.OkOnly)
StreamWriter.Close()
Me.Close()
End If
End Sub
End Class
Basically it looks like you're using Decimal.Compare wrong., See if this helps:
Do
str1 = InputBox("Enter your tax rate as a %" & vbCrLf & "between and including 0 - 10:", "Tax Rate", "", -1, -1)
If (str1 <> "") Then
If (Not IsNumeric(str1)) Then
MsgBox("You must enter a numeric tax rate", MsgBoxStyle.Exclamation, "Tax Rate Error")
Else
Dim decimal3 As Decimal = Convert.ToDecimal(str1)
If decimal3 < 0 Orelse decimal3 > 10 Then
MsgBox("You must enter a tax rate between and including 0% - 10%", MsgBoxStyle.Exclamation, "Tax Rate Error")
End If
End If
End If
Loop Until (IsNumeric(str1) Andalso (decimal3 <= 10 Andlso decimal3 >= 0))