6 Replies - 1859 Views - Last Post: 26 November 2012 - 08:28 PM Rate Topic: -----

#1 Giraffe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 12

Simple inventory program help-vb2010

Posted 24 November 2012 - 06:19 PM

I have to write a program that allows the user to order a product from inventory. The user selects the product, enters the quantity, and if there is sufficient quantity on hand to fufill the order, the price is retreived from the product array, the cost is calculated, order info is displayed on form, quantity order is deducted from the inventory array, and the order is written to an output file.

This is the code I've written so far. I feel like I'm running myself in circles and I'm not understanding how to set everything up. Any help with the code I've already written or especially code I've yet to write would be much appreciated. Maybe I could get some examples in order to help me see what my code should look like?



Option Strict On
Imports System.IO

Public Class Form1
				'Structure to hold product record info.
Structure ProductRecord
					'to hold product name.
	Dim strProduct As String
				'to hold quantity on hand.
	Dim strQuantity As String
					'to hold price.
	Dim strPrice As String

'******************************************************************************
'This procedure uses an array to list items in the list box.
'******************************************************************************
Dim lstProducts As ListBox
lstProducts.Items.Add= ("A54", "B42", "C21", "D87", "E54")

End Structure

'******************************************************************************
'This procedure reads data from a text file into an array of the ProductRecord
' structure.  It then writes out the array data to an output text file.
'******************************************************************************
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

				'declare variables for the input text file.
	Dim textFileIn As StreamReader
				'declare variables for the output text file.
	Dim textFileOut As StreamWriter
				'declare variables for the input and output file names.
	Dim strFileTextIn As String
	Dim strFileTextOut As String
				'declare a variable of the structure ProductRecord.
	Dim productRecord(5) As ProductRecord

	Dim intIndex As Integer
	Dim sngPrice As Single
	Dim sngQuantity As Single
	Dim intCount As Integer

					'assign the input data file. 
	strFileTextIn = "Textfile.txt"
				'test for existence of input data file.
	If File.Exists(strFileTextIn) Then
				'open the data file for input.
		textFileIn = File.OpenText(strFileTextIn)
				'initialize array index.
		intIndex = 0
				'test for end of input data file.
		Do Until textFileIn.Peek = -1
				'read fields from input data file into array of ProductRecord.
			With textFileIn
				productRecord(intIndex).strProduct = .ReadLine()
				productRecord(intIndex).strQuantity = .ReadLine()
				productRecord(intIndex).strPrice = .ReadLine()
				'assign price field to a price variable.
				sngPrice = CSng(productRecord(intIndex).strPrice)
				'calculate a price.
				sngPrice = CSng(sngPrice * sngQuantity)
				'assign the price to the price field in the ProductRecord array.
				productRecord(intIndex).strPrice = CStr(sngPrice)
			End With
				'increment the array index.
			intIndex += 1
		Loop
				'close the input data file.
		textFileIn.Close()

				'assign the output data file.
		strFileTextOut = "Textfile.txt"
				'create the data file for ouput.
		textFileOut = File.CreateText(strFileTextOut)
				'create or open existing file for appending new records.
		textFileOut = File.AppendText(strFileTextOut)
				'calculate the number of records read from file.
		intCount = intIndex - 2
				'write records to output data file.
		For intIndex = 0 To intCount
			MsgBox(productRecord(intIndex).strProduct)

			textFileOut.WriteLine(productRecord(intIndex).strProduct)
			textFileOut.WriteLine(productRecord(intIndex).strPrice)
			textFileOut.WriteLine(productRecord(intIndex).strQuantity)
		Next
				'close the output data file to write any remaining records to file.
		textFileOut.Close()
	Else
		MsgBox("File does not exist.")
	End If
End Sub


Private Sub lblPrice_Click(sender As System.Object, e As System.EventArgs) Handles lblPrice.Click

End Sub
End Class



I've also attached an executable copy of what the program should be when I'm done.Attached File  Debug(3).zip (33.46K)
Number of downloads: 97

Is This A Good Question/Topic? 0
  • +

Replies To: Simple inventory program help-vb2010

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Simple inventory program help-vb2010

Posted 24 November 2012 - 10:35 PM

Did you notice that little squiggly line under "lstProducts" in line 18? That tells you that there is an error there.
The error is "Declaration expected" (drive your mouse cursor over the part with the squiggly line to see what the error is).

It has to be a declaration because that's the only thing allowed between Structure and End Structure statements.

Your comment within the Structure declaration tells me you think you are writing a procedure, so you should move that line out to somewhere else, to inside a Sub or Function.

You should also look up ListBoxes and find out how to add multiple Items to it. That line will not do what you think it wil do.
Was This Post Helpful? 1
  • +
  • -

#3 Giraffe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 12

Re: Simple inventory program help-vb2010

Posted 26 November 2012 - 02:39 PM

'******************************************************************************
'This procedure uses an array to list items in the list box.
'******************************************************************************
Dim lstProducts() As ListBox
Dim strProduct As String
Dim MyProducts() As Integer
Dim i As Integer

MyProducts(0) = CInt(("A54"))
MyProducts(1) = CInt(("B42"))
MyProducts(2) = CInt(("C21"))
MyProducts(3) = CInt(("D87"))
MyProducts(4) = CInt(("E54"))

For i = 0 To 4

lstProducts.Items.Add(MyProducts(i))

Next i

End Sub



It's still not right. "lstProducts" is underlined and says items is not a member of system.array. What am I doing wrong?
Was This Post Helpful? 0
  • +
  • -

#4 Giraffe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-November 12

Re: Simple inventory program help-vb2010

Posted 26 November 2012 - 07:21 PM

Revised again, but still has errors. Help? :(

Option Strict On
Imports Microsoft.VisualBasic.ControlChars
Imports System.IO

Public Class Form1

				'Structure to hold product record info.
Structure ProductRecord
					'to hold product name.
	Dim strProduct As String
				'to hold quantity on hand.
	Dim strQuantity As String
					'to hold price.
	Dim strPrice As String

End Structure

'******************************************************************************
'This procedure reads data from a text file into an array of the ProductRecord
' structure.  It then writes out the array data to an output text file.
'******************************************************************************
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

				'declare variables for the input text file.
	Dim textFileIn As StreamReader
				'declare variables for the output text file.
	Dim textFileOut As StreamWriter
				'declare variables for the input and output file names.
	Dim strFileTextIn As String
	Dim strFileTextOut As String
				'declare a variable of the structure ProductRecord.
	Dim productRecord(5) As ProductRecord

	Dim intIndex As Integer
	Dim sngPrice As Single
	Dim sngQuantity As Single
	Dim intCount As Integer

					'assign the input data file. 
	strFileTextIn = "Textfile.txt"
				'test for existence of input data file.
	If File.Exists(strFileTextIn) Then
				'open the data file for input.
		textFileIn = File.OpenText(strFileTextIn)
				'initialize array index.
		intIndex = 0
				'test for end of input data file.
		Do Until textFileIn.Peek = -1
				'read fields from input data file into array of ProductRecord.
			With textFileIn
				productRecord(intIndex).strProduct = .ReadLine()
				productRecord(intIndex).strQuantity = .ReadLine()
				productRecord(intIndex).strPrice = .ReadLine()
				'assign price field to a price variable.
				sngPrice = CSng(productRecord(intIndex).strPrice)
				'calculate a price.
				sngPrice = CSng(sngPrice * sngQuantity)
				'assign the price to the price field in the ProductRecord array.
				productRecord(intIndex).strPrice = CStr(sngPrice)
			End With
				'increment the array index.
			intIndex += 1
		Loop
				'close the input data file.
		textFileIn.Close()

				'assign the output data file.
		strFileTextOut = "Textfile.txt"
				'create the data file for ouput.
		textFileOut = File.CreateText(strFileTextOut)
				'create or open existing file for appending new records.
		textFileOut = File.AppendText(strFileTextOut)
				'calculate the number of records read from file.
		intCount = intIndex - 2
				'write records to output data file.
		For intIndex = 0 To intCount
			MsgBox(productRecord(intIndex).strProduct)

			textFileOut.WriteLine(productRecord(intIndex).strProduct)
			textFileOut.WriteLine(productRecord(intIndex).strPrice)
			textFileOut.WriteLine(productRecord(intIndex).strQuantity)
		Next
				'close the output data file to write any remaining records to file.
		textFileOut.Close()
	Else
		MsgBox("File does not exist.")
	End If

'******************************************************************************
'This procedure uses an array to list items in the list box and combobox.
'******************************************************************************
				'Declare variables.
	Dim strQuantity() As String = {"2", "5", "3", "4", "6"}
	Dim lstProducts() As ListBox
	Dim strProduct() As String = {"A54", "B42", "C21", "D87", "E54"}
	Dim d As Integer

				'Set default combobox value to 1.
	Me.cboQuantity.Items.Insert(1, "Enabled")
				'Add items from array to list box and combo box.
	For d = 0 To strProduct.Length - 1

			lstProducts.Items.Add(strProduct(d))
			cboQuantity.Items.Add(strQuantity(d))

		Next d

End Sub
'******************************************************************************
'This procedure uses an array to list price of product in label.
'******************************************************************************
Private Sub lblPrice_Click(sender As System.Object, e As System.EventArgs) Handles lblPrice.Click
				'Declare variables and use array to list price in label.
	Dim strPrice() As String = {"5.40", "4.20", "5.70", "2.80", "1.30"}
	strPrice = Split(System.IO.File.ReadAllText("Textfile.txt"), vbCrLf)

	If lstProduct.SelectedItem Is "A54" Then
	lblPrice.Text = strPrice(0)

	ElseIf lstProduct.SelectedItem Is "B42" Then
	lblPrice.Text = strPrice(1)

	ElseIf lstProduct.SelectedItem Is "C21" Then
	lblPrice.Text = strPrice(2)
	ElseIf lstProduct.SelectedItem Is "D87" Then
	lblPrice.Text = StrPrice(3)"
	Else
	lstProduct.SelectedItem = "E54"
	lblPrice.Text = strPrice(4)
	End If

End Sub

Private Sub btnPrint_Click(sender As System.Object, e As System.EventArgs) Handles btnPrint.Click
'********************************************************************************************
'This procedure print previews the order.
'********************************************************************************************
		pvPrintPreviewSelected.Document = pdOrderFile
		pvPrintPreviewSelected.PrintPreviewControl.Zoom = 1
		pvPrintPreviewSelected.ShowDialog()
End Sub
'********************************************************************************************
'This procedure defines the print document for print previewing the order.
'********************************************************************************************
	Private Sub pdDocumentSelected_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
		  Handles pdDocumentSelected.PrintPage
		Dim sngX As Single
		Dim sngY As Single
		Dim fntPrintFont As New Font("Arial", 12)
		Dim sngLineHeight As Single = fntPrintFont.GetHeight + 2
		Dim lstProducts() As ListBox
		Dim strProduct() As String = {"A54", "B42", "C21", "D87", "E54"}
		Dim intListIndex As Integer
						'declare variables for the input text file.
	Dim OrderFileIn As StreamReader
				'declare variables for the output text file.
	Dim OrderFileOut As StreamWriter
				'declare variables for the input and output file names.
	Dim strFileOrderIn As String
	Dim strFileOrderOut As String


		sngX = e.MarginBounds.Left
		sngY = e.MarginBounds.Top
				'print heading
		e.Graphics.DrawString("Order", fntPrintFont, Brushes.Black, sngX, sngY)
		sngY += sngLineHeight
		sngY += sngLineHeight
				'display order.
		For intListIndex = 0 To strProduct.Items.Count - 1
			e.Graphics.DrawString(String.Format("{0,10}{1,-30} ", "Order:  ", strProduct.Items(intListIndex)).ToString, fntPrintFont, Brushes.Black, sngX, sngY)
			sngY += sngLineHeight
		Next
	End Sub

Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
				'Close the form.
	Me.Close()
End Sub

Private Sub btnOrder_Click(sender As System.Object, e As System.EventArgs) Handles btnOrder.Click
	Dim intQuantity As Integer
	Dim lstProducts.SelectedItem As ListBox
	Dim intCost As Integer = CInt((CDbl(cboQuantity.Text) * CDbl(lblPrice.Text)))
	Dim OrderFile As StreamWriter
	OrderFile = File.CreateText("OrderFile.txt")
				'Write order to the file.
Try
				'open the file.
	OrderFile = File.CreateText("OrderFile.txt")
				'write data to the file.
	OrderFile.WriteLine(lstProducts.SelectedItem)
	OrderFile.WriteLine(intQuantity)
	OrderFile.WriteLine(intCost)
Catch ex As Exception
	MessageBox.Show("Error:The file cannot be created.")

End Try
				'Prevent printing until after order is placed.
	btnPrint.Enabled = False
				'Close the file. 
	OrderFile.Close()
End Sub
End Class




Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Simple inventory program help-vb2010

Posted 26 November 2012 - 07:47 PM

View PostGiraffe, on 26 November 2012 - 08:21 PM, said:

Revised again, but still has errors. Help? :(/>

What errors are you getting now?
Can you show me the contents of the text file?
Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5315
  • View blog
  • Posts: 11,371
  • Joined: 02-June 10

Re: Simple inventory program help-vb2010

Posted 26 November 2012 - 08:23 PM

Giraffe: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#7 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Simple inventory program help-vb2010

Posted 26 November 2012 - 08:28 PM

OK... never mind the text file for now.

You have a ListBox named lstProducts. It gets declared (same as Dim) by placing it on the form in the Form [Design] window. This means that you do not need to Dim it again. As well, you have a line that Dims lstProducts as ListBox.SelectedItem, which is definitely a problem that will prevent compilation.

        '******************************************************************************
        'This procedure uses an array to list items in the list box and combobox.
        '******************************************************************************
        'Declare variables.
        Dim strQuantity() As String = {"2", "5", "3", "4", "6"}
' the following line declares lstProducts as an array of ListBoxes
'  and supercedes the declaration of your actual listbox.
        Dim lstProducts() As ListBox 
        Dim strProduct() As String = {"A54", "B42", "C21", "D87", "E54"}
        Dim d As Integer

        'Set default combobox value to 1.
        cboQuantity.Items.Insert(1, "Enabled")
        'Add items from array to list box and combo box.
        For d = 0 To strProduct.Length - 1

            lstProducts.Items.Add(strProduct(d))
            cboQuantity.Items.Add(strQuantity(d))

        Next d


Was everything above, after the comment, supposed to be a separate Sub? The comment seems to suggest it.

As well, see my comments within in the above code. You need to do some research on what constitutes an array, a control, and an element in an array. You also need to learn more about the scope of variables. Variables declared within a Sub or function are only usable within that Sub Or Function. They can be passed out by value (sending the contents to another Sub or Function so the value may be used without changing the variable itself), or passed out by Reference (sending the address of the variable to another Sub or Function so that the variable may be changed there)

When you Dim a variable in a different Sub or Function, you create a new variable of the same name, but without the data that was contained in any other variables of the same name.

That's enough for now. Do some research (the tutorials tlhIn`toq pointed out are excellent!), make some corrections, and come back with questions if you are still having problems.

This post has been edited by lar3ry: 26 November 2012 - 08:31 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1