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

Posted on 2007-12-04
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
Question by:jheg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Author Comment

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.

Author Comment

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)


    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 = ""
        If cdlg.FileName = "" Then
            ' User canceled.
                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()
                ListBox1.SelectedIndex = 0
            Catch exc As Exception
                ' Report all errors.
                MsgBox(exc.Message, MsgBoxStyle.Exclamation, "Read " & _
            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
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)

Author Comment

ID: 20424352
                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()
with your recommended code above and receiving the following message box error when we load the file

LVL 47

Accepted Solution

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?

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Dropdown list with sub group 10 43
Data Analysis 7 59
SSIS - Using VB.NET to parse XML file 11 42
Subtract dates in 6 34
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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.…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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