• 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

    • Latest leak spills potential details about the upcoming Google Pixel Watch 4 by Sagar Naresh Bhavsar Google is set to officially unveil the Pixel 10 series and the Pixel Watch 4 on August 20. The company shared a teaser video on YouTube, confirming the date and the devices. While details about the Pixel 10 have been emerging frequently, a new leak provides information about the upcoming Google smartwatch. Reliable leaker Evan Blass has shared multiple images, appearing to be promotional materials, on the social media platform X. The images allegedly hint at what the upcoming Google Pixel Watch 4, the latest Wear OS smartwatch from the company, could have in store. First up, Google Pixel Watch 4 will come with Gemini integration, which means it will be the default assistant going forward. As per the leak, the smartwatch will come in two sizes: 45mm and 41mm, similar to its predecessor. Both variants are purported to feature Google's Actua 360 display that can hit a peak brightness of 3,000 nits, allowing for better outdoor visibility, even in extreme sunlight. While the actual battery capacity isn't revealed, the 41mm Pixel Watch 4 is tipped to offer up to 30 hours of battery life, whereas the 45mm watch will offer up to 40 hours of juice. This includes the AOD (Always On Display) enabled. Charging is also getting a boost by 25% with the Quick Charge Dock. The Google Pixel Watch 4 will come with 40+ exercise modes, ECG, SpO2, breathing rate, heart rate variability, dual-frequency GPS, and loss of pulse detection. Google will also be offering two years of LTE data for free via Google Fi Wireless service on the purchase of the LTE model. The 45mm variant of the Pixel Watch 4 is rumored to be launched in three colors, while the 41mm is shown to have an extra color. Images via Evan Blass on X
    • Here ya go : https://shop.royalmail.com/special-stamp-issues/monty-python 
    • What is happening in the WNBA!    
  • Recent Achievements

    • Week One Done
      harveycoleman123 earned a badge
      Week One Done
    • First Post
      EzraNougat earned a badge
      First Post
    • One Month Later
      westDvina earned a badge
      One Month Later
    • Community Regular
      Bern@rd went up a rank
      Community Regular
    • Week One Done
      Joey Solo earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      664
    2. 2
      +FloatingFatMan
      196
    3. 3
      ATLien_0
      154
    4. 4
      Xenon
      132
    5. 5
      wakjak
      101
  • Tell a friend

    Love Neowin? Tell a friend!