Question List Box Issue

sanctity

Member
Joined
Jun 2, 2012
Messages
7
Programming Experience
1-3
Private Sub btnEOD_Click
    ' My issue here is that when the items in my list box are being added it doesn't them together (Decx = decx + decSubTotal) 
    ' It is multiplying the last entry by the amount of entries entered. (All entries are ranndom and vary in quantity. I am trying to make an end of day sales total.)

    decItemCount = CDec(lstTotalSales.Items.Count)

    Do Until decItemCount = 0
        decItemCount -= 1
        decx = decx + decSubTotal
    Loop

    strX = decx.ToString("C")
    lblX.Text = strX

    ' This part adds the second numerical variable in the list box

    decItemCount2 = CDec(lstTotalSales.Items.Count())

    Do Until decItemCount2 = 0
        decItemCount2 -= 1
        decY = decY + decTotal
    Loop

    strY = decY.ToString("C")
    lblY.Text = strY
End Sub
 
Last edited by a moderator:
x = Convert.ToInt16(TextBox1.Text)
Dim x As Short
If Not Short.TryParse(TextBox1.Text.Trim, x) Then
    'Parse failed, should tell the user somehow
    Return
End If
'Now use x

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
intTotal = intTotal + 4
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    intTotal += 4
End Sub


This is everything; I truly appreciate all of the help.
Essentially this is more of what you're probably trying to do here, this is a project I put together to illustrate what we're talking about. It increments 2 totals with each addition & includes a way to remove an item from the listbox and decrement those totals:
Option Explicit On
Option Strict On
Option Infer Off

Public Class MainForm

    'Class level variables to hold the totals
    Private m_TotalItems As Integer = 0I
    Private m_TotalCost As Decimal = 0D

    Private Sub AddButton_Click(sender As System.Object, e As System.EventArgs) Handles AddButton.Click
        If Item1NumericUpDown.Value > 0D Then
            'If there are any quantities of "Item 1" create a new ItemClass for it, add the class to the ListBox then increment the totals
            'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
            Dim newItem As New ItemClass("Item 1", CInt(Item1NumericUpDown.Value), CDec(Item1PriceLabel.Tag))
            ItemsListBox.Items.Add(newItem)
            m_TotalItems += newItem.Quantity
            m_TotalCost += newItem.GetCost()
            Item1NumericUpDown.Value = 0D
        End If
        If Item2NumericUpDown.Value > 0D Then
            'If there are any quantities of "Item 2" create a new ItemClass for it, add the class to the ListBox then increment the totals
            'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
            Dim newItem As New ItemClass("Item 2", CInt(Item2NumericUpDown.Value), CDec(Item2PriceLabel.Tag))
            ItemsListBox.Items.Add(newItem)
            m_TotalItems += newItem.Quantity
            m_TotalCost += newItem.GetCost()
            Item2NumericUpDown.Value = 0D
        End If
        If Item3NumericUpDown.Value > 0D Then
            'If there are any quantities of "Item 3" create a new ItemClass for it, add the class to the ListBox then increment the totals
            'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
            Dim newItem As New ItemClass("Item 3", CInt(Item3NumericUpDown.Value), CDec(Item3PriceLabel.Tag))
            ItemsListBox.Items.Add(newItem)
            m_TotalItems += newItem.Quantity
            m_TotalCost += newItem.GetCost()
            Item3NumericUpDown.Value = 0D
        End If
        If Item4NumericUpDown.Value > 0D Then
            'If there are any quantities of "Item 4" create a new ItemClass for it, add the class to the ListBox then increment the totals
            'The item's price that's convertible to a decimal is in the PriceLabel's Tag property
            Dim newItem As New ItemClass("Item 4", CInt(Item4NumericUpDown.Value), CDec(Item4PriceLabel.Tag))
            ItemsListBox.Items.Add(newItem)
            m_TotalItems += newItem.Quantity
            m_TotalCost += newItem.GetCost()
            Item4NumericUpDown.Value = 0D
        End If

        'Update the label's on the form
        TotalItemsLabel.Text = m_TotalItems.ToString
        TotalCostLabel.Text = m_TotalCost.ToString("c")
    End Sub

    Private Sub RemoveToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RemoveToolStripMenuItem.Click
        'Get the Listbox's selected item, cast it to type ItemClass
        Dim selItem As ItemClass = CType(ItemsListBox.SelectedItem, ItemClass)

        'Decrement the total's
        m_TotalItems -= selItem.Quantity
        m_TotalCost -= selItem.GetCost()

        'Remove the item from the ListBox
        ItemsListBox.Items.Remove(selItem)

        'Update the total's on the form
        TotalItemsLabel.Text = m_TotalItems.ToString
        TotalCostLabel.Text = m_TotalCost.ToString("c")
    End Sub

    Private Sub ItemsListBox_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ItemsListBox.MouseDown
        ItemsListBox.SelectedIndex = ItemsListBox.IndexFromPoint(e.Location)
    End Sub

    Private Sub ItemsListBox_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ItemsListBox.SelectedIndexChanged
        RemoveToolStripMenuItem.Enabled = ItemsListBox.SelectedIndex > -1I
    End Sub

End Class

'This class is used in the ListBox
Friend Class ItemClass

    Friend Sub New(Itm As String, Qty As Integer, Prc As Decimal)
        Me.ItemName = Itm
        Me.Quantity = Qty
        Me.Price = Prc
    End Sub

    Friend Property ItemName As String = String.Empty
    Friend Property Quantity As Integer = 0I
    Friend Property Price As Decimal = 0D

    Friend Function GetCost() As Decimal
        Return Me.Quantity * Me.Price
    End Function

    'This is how we control what text is displayed in the ListBox, the LB call's the item's ToString() method and this is what gets returned from that call
    Public Overrides Function ToString() As String
        Return String.Format("{0}: {1} @ {2} = {3}", Me.ItemName, Me.Quantity.ToString("000"), Me.Price.ToString("c"), Me.GetCost.ToString("c"))
    End Function
End Class
 

Attachments

  • WindowsApplication1.zip
    18.2 KB · Views: 16
Yes. You do have to take care that the value of decRunningTotal is not altered at any other point in the program. And lest the program is closed (accidentally or otherwise) it would make sense to write the value to a file every time it's updated as a back up.
 
Back
Top