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.
Debug(3).zip (33.46K)
Number of downloads: 24

New Topic/Question
Reply



MultiQuote






|