Solved

Need to load a text file and parse 4 fields from each line and place into 4 separate listboxes

Posted on 2007-12-04
6
151 Views
Last Modified: 2013-11-27
Creating a Visual basic project that contains a load buton and 4 listboxes. When load button is clicked, need to prompt user for file location and load file. Once file is loaded, need to parse out 4 fields from each row and place in respective listboxes.  The fields needed are MDEntryPx and MDEntrySize to provide price and size. Then  MDEntrytype to determine whether it is 0 or 1. If it is 0 the price and size go to the two left listboxes lbbidprice and lbbidsize and if 1 the price and size goe to the two right listboxes lbaskprice and lbasksize. We also need the MDPriceLevel to determine which level to insert into listboxes.

 The fields are  separated by the ' | ', below is one line from file

-I 10-12 14:08:42.629 3936 4 null Received IR MessageID=100|MsgType=X|MsgSeqNum=2057577|SendingTime=20071009-13:32:33.230|NoMDEntries=1|MDUpdateAction[0]=Change|MDEntryType[0]=Bid|Symbol[0]=MGMAT|CFICode[0]=OC|MaturityMonthYear[0]=20080119|StrikePrice[0]=100|SecurityDesc[0]=MGM|MDEntryPx[0]=7.9|MDEntrySize[0]=60|MDPriceLevel[0]=2|QuantityCustomer[0]=0|Customer[0]=False
0
Comment
Question by:jheg
  • 3
  • 2
6 Comments
 

Author Comment

by:jheg
Comment Utility
The specific problem we are having is when the load button is clicked to be able show a dialog box and once the file is selected to be able to parse out certain fields from each line.
0
 

Author Comment

by:jheg
Comment Utility
We are able to create a dialog box to open the file but cannot parse out only the fileds we need and place in a listbox, below is the code for the load button

Public Class Form1

 
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
        e As System.EventArgs) Handles MyBase.Load


    End Sub
    Private Function DataFile() As String
        Dim file_name As String = Application.StartupPath

        If file_name.EndsWith("\bin") Then file_name = file_name.Remove(file_name.Length - 4, 4)
        file_name &= cdlg.FileName
        Return file_name
    End Function
    Private Sub btnPlay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlay.Click
        Dim x As Int16
        Do While Not (ListBox1.Items Is Nothing)

        Loop

    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        cdlg.Filter = "Log File (*.log) | *.log"
        cdlg.InitialDirectory = ""
        cdlg.ShowDialog()
        If cdlg.FileName = "" Then
            ' User canceled.
        Else
            Try
                Dim file_name As String = cdlg.FileName
                Dim stream_reader As New IO.StreamReader(file_name)
                Dim line As String
                Dim i As Int16

                ' Read the file one line at a time.
                line = stream_reader.ReadLine()
                Do While Not (line Is Nothing)
                    ' Trim and make sure the line isn't blank.
                    line = line.Trim()

                    If line.Length > 0 Then _
                        ListBox1.Items.Add(line) ' parse the code in this area
                    ' Get the next line.
                    line = stream_reader.ReadLine()
                Loop
                ListBox1.SelectedIndex = 0
                stream_reader.Close()
            Catch exc As Exception
                ' Report all errors.
                MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & _
                    "Error")
            End Try
        End If
    End Sub

    Private Sub cdlg_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles cdlg.FileOk

    End Sub
End Class
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
I would modify the loop, as well as using the Split() function to parse the individual items....

    Do Until stream_reader.Peek = -1
        Dim Items() As String = stream_reader.ReadLine.Split("|")
        ' MDEntryPx is the 13th item in the array, and would be obtained using this....
        Dim MDEntryPx As String = Items(12)
    Loop
0
 

Author Comment

by:jheg
Comment Utility
Replaced
                Do While Not (line Is Nothing)
                    ' Trim and make sure the line isn't blank.
                    line = line.Trim()

                    If line.Length > 0 Then _
                        ListBox1.Items.Add(line) ' parse the code in this area
                    ' Get the next line.
                    line = stream_reader.ReadLine()
                Loop
with your recommended code above and receiving the following message box error when we load the file

INDEX WAS OUTSIDE THE BOUNDS OF THE ARRAY.
0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
You probably ran upon a line that didn't have all the fields. Are the line fields always the same?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now