Reading Sequential Files

Loading a list box by reading a file

Page 1 of 1

2 Replies - 2690 Views - Last Post: 02 March 2010 - 03:14 PM Rate Topic: -----

#1 Perplexedwon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-March 10

Reading Sequential Files

Posted 02 March 2010 - 10:21 AM

In this assignment, I am suppose to create a module level array name Product and populate a list box with the contents of a file (ItemInfo.txt) The file contains the Item ID and price of the item. (Line 1 is an item, Line 2 is a price for Line 1, Line 3 is an item, Line 4 is a price for Line 3, and so on.) The problem i'm having is when i populated the list box, I only wanted to populate the list box with the items, not everyline from the file. I guess I wanted to put elements [0], [2], [4], [6], and [8] from the Product array in the list box, but I am putting everything in there. What am I missing?

here is my code so far:
' Name:         Glovers Project
' Purpose:      Displays the price of an item
' Programmer:   <Paul J. Williams> on <February 27, 2010>

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

    Structure Product
        Public strItem As String
        Public dblPrice As Double
    End Structure
    ' Declare a 5 element module level array for the Product Structure

    Private ProductList(4) As Product
    Dim strSearchForID As String
    Dim intSubscript As Integer


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


    Private Sub lstNumbers_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstNumbers.Enter

    End Sub

    Private Sub lblPrice_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles lblPrice.Enter

    End Sub

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim inFile As IO.StreamReader
        Dim strLine As String

        ' Determine if the file exist


        If IO.File.Exists("ItemInfo.txt") Then
            inFile = IO.File.OpenText("ItemInfo.txt")

            ' Process the look until the end of the file

            Do Until inFile.Peek = -1

                ' Read a line

                strLine = inFile.ReadLine

                ' Populate the list box

                lstNumbers.Items.Add(strLine)

            Loop

            ' Close the file

            inFile.Close()
        Else
            MessageBox.Show("Can not locate the ItemInfo.txt file", "Glovers Industries", _
                            MessageBoxButtons.OK, MessageBoxIcon.Information)

        End If

    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Reading Sequential Files

#2 egof  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 20
  • View blog
  • Posts: 132
  • Joined: 27-March 09

Re: Reading Sequential Files

Posted 02 March 2010 - 11:49 AM

Your wanting it to skip every other line?
In your reading loop use a variable to decide when to skip
dim skipLine as boolean

skipLine=false

Do Until inFile.Peek = -1 
     strLine = inFile.ReadLine 
     if skipLine=false then
         ' Populate the list box 
 
         lstNumbers.Items.Add(strLine) 
         skipLine=true
    else
         skipLine=false
    end if
Loop 


Was This Post Helpful? 0
  • +
  • -

#3 Perplexedwon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 01-March 10

Re: Reading Sequential Files

Posted 02 March 2010 - 03:14 PM

View Postegof, on 02 March 2010 - 10:49 AM, said:

Your wanting it to skip every other line?
In your reading loop use a variable to decide when to skip
dim skipLine as boolean

skipLine=false

Do Until inFile.Peek = -1 
     strLine = inFile.ReadLine 
     if skipLine=false then
         ' Populate the list box 
 
         lstNumbers.Items.Add(strLine) 
         skipLine=true
    else
         skipLine=false
    end if
Loop 





Thanks a million, that did it. I was trying to find a way to display the array element by element with the subscript, but this was much easier. Thanks again!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1