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
155 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

861 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