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
159 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
ID: 20411055
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
ID: 20412867
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)
ID: 20414884
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
ID: 20424352
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
ID: 20425689
You probably ran upon a line that didn't have all the fields. Are the line fields always the same?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

680 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