Im trying to make an application that collects how many items were sold and which items they were, I was thinking something like this for checking the upc number, to see if it matches the names in the “files”

Select Case TextBox1.Text
            ' 0921115150 “Dark Chocolate Truffles”, 2221001501 “ mint mintaways”, 6652300415 “Chocolate Covered Cherries”,
            ' 0255541028 “Malted Milk Balls”, 0921115141 “Chocolate Covered Raisins “
            Case "0921115150 ", " 2221001501 ", " 6652300415 ", " 0255541028 ", " 0921115141 "
                MsgBox("Correct #")
            Case Else
                MsgBox("Must be either: 0921115150 /2221001501 /6652300415 /0255541028 /0921115141 ", MsgBoxStyle.Exclamation) ' Error message displayed
        End Select

Thanks to codeorder, he helped me shorten my code for this part

then another textbox to allow how many of that product was sold, and off course a “sold” button.

I was thinking about making a list box that will hold everything, When I hit the sold button it’ll add the upc number, quantity sold, price per quantity, and how much all together that order will cost

Private Sub btnsold_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsold.Click
        ListBox1.Items.Add(TextBox1.Text & ":" & TextBox2.Text) ' adds the items to listBox.
    End Sub

This is what I was kinda talking about, you can see there are multiple UPC codes that are the same but with different quantities)

UPC Quantity Price Total
0921115150 17 3.50 59.50
2221001501 5 17.25 86.25
6652300415 32 3.25 104.00
2221001501 3 20.00 60.00
0255541028 12 1.85 22.20
0921115150 14 32.56 455.84
2221001501 3 25.26 75.78


I would like it to be saved to a data file (text file). There will be two buttons for this, a save button and a load button, I already know how to do this part, but what I need help on is how do I add things to a listbox in a neat spaced out order and have the price per quantity already in it (after the quantity of course) and be able to multiply how many quantities there is with the price and give a total price for that purchase. Could someone help me out

Or is the calculating things in a list box not possible and I should change my “sold” button a lil bit so when I enter the upc code, it’ll automatically take the quantity of that order and times it by how much it costs and makes a new list on the list box for it, or is that even possible?

Recommended Answers

All 10 Replies

wow apparently spacing doesn't count on these threads, but what I was trying to do was space out the UPC, Quantity, Price, and Total so they would look neat and and spaced out and not all cluttered together

For example of what it was suppose to look like (for the first one)
UPC: 0921115150
Quantity: 17
Price: 3.50
Total: 59.50

Dear Twigan1015,

I am sorry but confused what you are asking here. What i understand is...

You have four column/Field UPC, Quantity, Price and Total. You want to add all these four column's data into a listbox as listitem. And after that you will save it into a text file. And whenever it needed the data of text files, you will retrieve into a listbox.

For calculating purpose which field/column' data you will provide as input data? As i understand you will provide only UPC and Quantity. What about price of quantity?

Let us know it clearly.

Or maybe itll just be better if we put it in a listview, all I need it to did is be neat and orderly when it comes up

But you got the have four column/Field UPC, Quantity, Price and Total right, and add them into a listbox ( or would it just be easier to add them into a listview?). I want them to be neat like in columns, so you can see everything clearly without everything bunched up together.

What I want to do is when I hit the sold button it will add the upc code and the quantities to the listbox (or the listview, I don’t know which one would be better), then somehow it will know which upc code it is, then it will put the price per quantity on a new column (for that specific upc code) right next to the quantities column and then a total price column right next to the price per quantity column

The price per quanity will already be in the program somewhere, so all the user has to do is add the upc code in and the quantities in and the application will show how many each one will cost and how much the total will cost, but I don’t know how it will know which upc code it is and know where it needs to go, this is where im having trouble on

I hope it made it clearer of what im trying to do

Before you can run the provided code, you will need a File which loads all UPCs and Prices from it to 2 ArrayLists.
I saved mine to: Private myUPCsAndPricesFile As String = "C:\myUPCsAndPricesFile.txt" .

The file content should contain both UPC and individual Price for each item, and should be separated by a single space (" ") .

0921115150 3.50
2221001501 17.25
6652300415 3.25
2221001501 20.00
0255541028 1.85
0921115150 32.56
2221001501 25.26

As for what makes the app. tick, see if this helps.
1 ListView, 2 TextBoxes(TextBox1=UPC, TextBox2=Quantity), 1 Button

Public Class Form1
    Private myUPCsAndPricesFile As String = "C:\myUPCsAndPricesFile.txt"
    Private arlUPC, arlPrices As New ArrayList '// store UPCs and Prices in 2 ArrayLists.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With ListView1 '// customize ListView.
            .Columns.Add("UPC", 100) : .Columns.Add("Quantity", 100) : .Columns.Add("Price", 100) : .Columns.Add("Total", 100)
            .View = View.Details : .FullRowSelect = True
        End With
        If IO.File.Exists(myUPCsAndPricesFile) Then '// check if File.Exists.
            For Each fileLine As String In IO.File.ReadAllLines(myUPCsAndPricesFile) '// loop thru all file lines.
                If Not fileLine = "" Then '// make sure there is a value in file line.
                    arlUPC.Add(fileLine.Split(" "c).GetValue(0)) '// .Split file line by the " "(space), and add first value to the UPC ArrayList.
                    arlPrices.Add(fileLine.Split(" "c).GetValue(1)) '// add second value to the Prices ArrayList.
                End If
            Next
        Else '// if File does not Exist.
            MsgBox("This application will not run without the system file:" & vbNewLine & myUPCsAndPricesFile, MsgBoxStyle.Critical)
            Application.Exit()
        End If
        Button1.Text = "Add New"
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            MsgBox("UPC must be entered in TextBox1", MsgBoxStyle.Critical)
            Exit Sub '// skip remaining code in this Sub.
        End If
        If TextBox2.Text = "" Then
            MsgBox("Quantity Amount must be entered in TextBox2", MsgBoxStyle.Critical)
            Exit Sub '// skip remaining code in this Sub.
        End If
        For i As Integer = 0 To arlUPC.Count - 1 '// loop thru all UPCs in ArrayList.
            If arlUPC(i).ToString = TextBox1.Text Then '// if a match to your TextBox UPC.
                Dim lvItem As New ListViewItem(arlUPC(i).ToString) '// add UPC to first column.
                With lvItem.SubItems
                    .Add(TextBox2.Text) '// add Quantity from Quantity TextBox.
                    .Add(CDbl(arlPrices(i)).ToString("c")) '// add the price from "i" since it is consecutive to UPC. "c" for Currency Format.
                    .Add(CDbl(CDbl(TextBox2.Text) * CDbl(arlPrices(i).ToString)).ToString("c")) '// multiply Quantity & Price to get Total.
                    ListView1.Items.Add(lvItem) '// add item to ListView.
                    Exit For '// exit loop since done.
                End With
            End If
        Next
    End Sub
End Class

That works perfect, thanks codeorder, but I forgot a few things and kinda ran into a couple of problems, nothing to do with your code of course. The original code I was going to use for saving and loading was for a listbox, but it doesn’t work for a listview, I did change the names where there were errors, figuring it was an easy quick fix, but the button don’t work, heres my code for the save button

Private Sub ButtonLoadObjectCollection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
        Dim DeSerializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FileStream As New System.IO.FileStream("C:\ListBoxData.lbd", IO.FileMode.Open)

        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(DeSerializer.Deserialize(FileStream))
    End Sub

And the load button

Private Sub ButtonSaveObjectCollection_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim Serializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FileStream As System.IO.FileStream = New 
System.IO.FileStream("C:\ListBoxData.lbd", IO.FileMode.Create)

        Dim ItemArray(ListBox1.Items.Count - 1) As Object
        ListBox1.Items.CopyTo(ItemArray, 0)
        Serializer.Serialize(FileStream, ItemArray)
    End Sub

As you can see there for listbox and not for listview, do I need to make a whole new code to save and load different listviews or is it just a simple little change can be done?

I kinda forgot to ask if there was a way to add up all the totals for a grand total, to see how much profit was made for that one day, is that possible?

Also I need to make the application to use some form of repeating program instruction, I don’t know exactly how to do this, but I was thinking about making it clear the textboxes when I hit the “add new” button , but I don’t know if it is suppose to go inside the add new button, or somewhere else

Private Sub ClearForm()

        TextBox1.Clear()
        TextBox2.Clear()

    End Sub

Or is there something better that I could use for a repeating program?

To save and load a ListView with columns, view this thread.

To get a total for the day, see if this helps.

With ListView1
            If Not .Items.Count = 0 Then '// check if items in ListView.
                Dim dTotal As Double = 0
                For Each itm As ListViewItem In .Items '// loop thru items.
                    '// check if column 4 item has a value and add it to your total.
                    If Not itm.SubItems(3).Text = "" Then dTotal += CDbl(itm.SubItems(3).Text)
                Next
                MsgBox(dTotal.ToString("c")) '// display result.
            End If
        End With

Making a "repeating program", whatever that is, add a Timer, enable it on Form1_Load and place a MsgBox in the Timer's_Tick event.

Ok thanks, I figured out the save and load listview
but I don’t know if im putting the grand total code in the wrong area, but its not doing anything, its kinda like the code doesn’t exist, its weird, I put it in the form1_load code, but it didnt worked and it didn’t show a msg box of the grand total, is there a way to make a button that makes the grand total, if that will be easier or maybe its just im putting the code in wrong, all im doing is copying and pasting the code right into form1_load right under the first “end with” statement, that’s the right place to put it right?

>>I kinda forgot to ask if there was a way to add up all the totals for a grand total, to see how much profit was made for that one day, is that possible?

Without items in the ListView, you cannot get a "how much profit was made for that one day", now can you?

The grand total code should be placed in a Button.Click or can even be added to the same Sub that adds items to the ListView. If the same Sub, I would remove the MsgBox and have a Label instead. Also, make sure it is right after the add items code and not before.

yeah true sorry, it was 4 in the morning and I wasnt thinking clearly and put the code into a button and everything works perfectly now, thanks

Member Avatar for Unhnd_Exception

List View Item is marked as Serializable.

You do not need to serialize it as an Object() but instead an array of ListViewItem()

Just thought I'd throw it in there.

Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSave.Click
        Dim Serializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FileStream As New System.IO.FileStream("C:\listviewdata.lvd", IO.FileMode.Create)

        Dim Items(ListView1.Items.Count - 1) As ListViewItem
        ListView1.Items.CopyTo(Items, 0)

        Serializer.Serialize(FileStream, Items)

        FileStream.Dispose()

    End Sub

    Private Sub ButtonLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLoad.Click
        Dim DeSerializer As New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
        Dim FileStream As New System.IO.FileStream("C:\listviewdata.lvd", IO.FileMode.Open)

        ListView1.Items.Clear()

        Dim ListViewItems As ListViewItem() = CType(DeSerializer.Deserialize(FileStream), ListViewItem())

        ListView1.Items.AddRange(ListViewItems)

        FileStream.Dispose()
    End Sub
commented: nice! :) +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.