Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 165
  • Last Modified:

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

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
jheg
Asked:
jheg
  • 3
  • 2
1 Solution
 
jhegAuthor Commented:
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
 
jhegAuthor Commented:
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
 
Wayne Taylor (webtubbs)Commented:
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
 
jhegAuthor Commented:
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
 
Wayne Taylor (webtubbs)Commented:
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now