• 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

  • 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

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

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

    • No registered users viewing this page.
  • Posts

    • I really do like the design of the iPhone 16 Pro Max. It is quite nice.
    • If the entire industry adhered to fundamental principles of Microsoft's Next-Generation Secure Computing Base (NGSCB), there might not have been a basis of the existence of this article.
    • Ashes of the Singularity II announced promising even larger battles, new faction, and more by Pulasthi Ariyasinghe A sequel for the 2016-released Ashes of the Singularity has officially been confirmed, and the original developer and publisher duo, Oxide Games and Stardock Entertainment, are back for this release too. Check out the debut trailer above. Ashes of the Singularity II is under development with a significantly expanded team compared to the original, according to today's announcement. It will be adding in a range of fresh units and gameplay systems for real-time strategy fans to dive into. More options for strategic gameplay, more intricate base building, and better enemy AI are listed as features for the new game as well. "Ten years ago, we set a new, literal benchmark for RTS games with massive battles and groundbreaking technology," said Brad Wardell, CEO of Stardock Entertainment. "With Ashes of the Singularity II, we’re raising the bar even higher, delivering strategic gameplay depth that RTS fans have always dreamed of.” The storyline of Ashes of the Singularity II will take place in 2031, with a complete single-player campaign, as well as cooperative and competitive multiplayer modes, being confirmed as features today. Moreover, a third faction is joining the fray this time, with Humans joining the fight alongside the original games' Post-Human Coalition and the Substrate. "Obviously, in the first game, the number one request was to have a human faction," adds Wardell. “Back then, we just couldn’t support having thousands of organic, walking, squishy people in the world and thus had to design in favor of machines. We’re really excited to bring the humans into the war and watch how they fare against the massive mechanical armies of the Substrate and PHC.” Ashes of the Singularity II is targeting a broad 2026 release window, with the team hoping to launch the title as the original celebrates its 10th anniversary. Its Steam store page is already up for wishlisting over here. Disclaimer: Neowin's relationship to Stardock
    • How exactly is a FREE image generation model a... Ponzi scheme?!?! I don't love all of these Gen-AI things flooding the market but there's nothing "ponzi" about it.
  • Recent Achievements

    • One Month Later
      SamZrize earned a badge
      One Month Later
    • Week One Done
      SamZrize earned a badge
      Week One Done
    • One Year In
      SamZrize earned a badge
      One Year In
    • One Year In
      barracuda earned a badge
      One Year In
    • One Month Later
      barracuda earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      723
    2. 2
      +FloatingFatMan
      189
    3. 3
      ATLien_0
      180
    4. 4
      Xenon
      113
    5. 5
      neufuse
      109
  • Tell a friend

    Love Neowin? Tell a friend!