• 0

How can I multiply items in a listbox?


Question

5 answers to this question

Recommended Posts

  • 0

Where are you getting the prices from? To create a sub total when the add button is clicked you just need to set something up like:

Dim subTotal as Decimal = Integer.Parse(quantityTextBox.Text) * <where ever the price values is defined>
subTotalListBox.Items.Add(subTotal)

Otherwise you could pull items back off the listboxes but that seems like a bad way of doing it

Dim subTotal as Decimal = Integer.Parse(quantityListBox.Items.Item(quantityListBox.Items.Count - 1).ToString()) * Decimal.Parse(PriceListBox.Items.Item(priceListBox.Items.Count - 1).ToString())
subTotalListBox.Items.Add(subTotal)

If you want the values to be entered as currency instead of just numbers you can use the ToString method when you add the decimal to your subtotals listbox

subTotalListBox.Items.Add(subTotal.ToString("c2")

And parse will throw an exception when it fails so if you're expected to handle bad user input you will have to catch the exception or use tryparse instead

Link to comment
Share on other sites

  • 0

yea i know it should be doing that but i cant figure out how to get the sub total so im using the price until i get it working. i guess i should have shown my code.

Public Class frmJoeSports

	Public g_ItemIDs(10) As String
	Public g_ItemDescriptions(10) As String
	Public g_ItemPrices(10) As String

	Private Sub frmJoeSports_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		g_ItemIDs(0) = "1000"
		g_ItemIDs(1) = "2000"
		g_ItemIDs(2) = "3000"
		g_ItemIDs(3) = "4000"
		g_ItemIDs(4) = "5000"
		g_ItemIDs(5) = "6000"
		g_ItemIDs(6) = "7000"
		g_ItemIDs(7) = "8000"
		g_ItemIDs(8) = "9000"
		g_ItemIDs(9) = "1100"
		g_ItemIDs(10) = "1110"
		g_ItemDescriptions(0) = "Shoes"
		g_ItemDescriptions(1) = "Pants"
		g_ItemDescriptions(2) = "Soccer Ball"
		g_ItemDescriptions(3) = "Football"
		g_ItemDescriptions(4) = "BasketBall"
		g_ItemDescriptions(5) = "Netball"
		g_ItemDescriptions(6) = "Shirt"
		g_ItemDescriptions(7) = "Gold Clubs"
		g_ItemDescriptions(8) = "Bowling Ball"
		g_ItemDescriptions(9) = "Cricket Bat"
		g_ItemDescriptions(10) = "Tennis Racket"
		g_ItemPrices(0) = "50"
		g_ItemPrices(1) = "40"
		g_ItemPrices(2) = "80"
		g_ItemPrices(3) = "70"
		g_ItemPrices(4) = "30"
		g_ItemPrices(5) = "50"
		g_ItemPrices(6) = "110"
		g_ItemPrices(7) = "300"
		g_ItemPrices(8) = "150"
		g_ItemPrices(9) = "80"
		g_ItemPrices(10) = "20"
	End Sub

	Private Sub btnAddItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddItem.Click
		Dim sTargetItem As String
		Dim bFoundItem As Boolean
		Dim iFoundPos As Integer
		Dim iCounter As Integer

		sTargetItem = CDbl(txtItemID.Text)
		bFoundItem = False
		iCounter = 0
		Do While bFoundItem = False
			If sTargetItem = g_ItemIDs(iCounter) Then
				bFoundItem = True
				iFoundPos = iCounter
			End If
			iCounter = iCounter + 1
		Loop

		lstItemID.Items.Add(g_ItemIDs(iFoundPos))
		lstDescription.Items.Add(g_ItemDescriptions(iFoundPos))
		lstPrice.Items.Add(g_ItemPrices(iFoundPos))
		lstQty.Items.Add(txtQty.Text)
	End Sub

	Private Sub btnTotals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTotals.Click
		Dim iNumItems As Integer
		Dim iTotal As Integer
		Dim iCounter As Integer
		Dim iAnItem As Integer

		iNumItems = lstPrice.Items.Count
		iTotal = 0

		For iCounter = 0 To (iNumItems - 1)
			iAnItem = lstPrice.Items(iCounter)
			iTotal = iTotal + iAnItem
		Next

		lblTotal.Text = iTotal
		lblGST.Text = iTotal / 10

	End Sub

	Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
		lstItemID.Items.Clear()
		lstDescription.Items.Clear()
		lstQty.Items.Clear()
		lstPrice.Items.Clear()
		lstSubTotal.Items.Clear()
		txtItemID.Text = ""
		txtQty.Text = ""
		lblTotal.Text = ""
		lblGST.Text = ""
	End Sub

	Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
		Me.Close()
	End Sub
End Class

Link to comment
Share on other sites

  • 0

In "Sub btnAddItem_Click" you have:

		lstPrice.Items.Add(g_ItemPrices(iFoundPos))
		lstQty.Items.Add(txtQty.Text)

just multiply "g_ItemPrices(iFoundPos)" and txtQty.Text using the "Parse" methood that slickice11 posted and add it to the subTotal list.

Link to comment
Share on other sites

  • 0

excellent thanks sorry i didnt even realise slickice11 post before, man i was out for a couple of days, out of my mind haha.

anyway thanks that worked perfectly.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.