Results 1 to 6 of 6

Thread: [RESOLVED] Dynamically populate listview from file?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    16

    Resolved [RESOLVED] Dynamically populate listview from file?

    Hello,
    I am trying to figure out how to dynamically populate a listview with data from a file, except I only want specific lines and columns from that file. I have code right now that works to populate the listview from the file, but it writes the whole file and I only want specific lines and columns. Here is the code I currently have to populate the listview when the form loads:


    VB 2010 Express Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("STFRWSAL.REP")
    3.  
    4.             MyReader.TextFieldType = FileIO.FieldType.Delimited
    5.  
    6.             MyReader.SetDelimiters(",")
    7.  
    8.             Dim currentRow As String()
    9.  
    10.             While Not MyReader.EndOfData
    11.                 currentRow = MyReader.ReadFields()
    12.                 Dim item As ListViewItem
    13.                 item = ListView1.Items.Add(currentRow(0))
    14.                 Dim ix%
    15.                 For ix = 1 To UBound(currentRow)
    16.                     item.SubItems.Add(currentRow(ix))
    17.                 Next
    18.             End While
    19.         End Using
    20.     End Sub

    I am new to VB so any suggestions or help would be greatly appreciated.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Dynamically populate listview from file?

    can you show us the file? + explain which rows + columns you want

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    16

    Re: Dynamically populate listview from file?

    I copied the file's data to a .txt file and added it as an attachment becuase the original .REP file can only be opened with certain programs, but anyways I am trying to get the text that starts after the 4 digit number code up until the second identifiers (the B9, Bd, BX, and such) that start at the 34th space. I also want to ignore the fields that have no text directly after the 4 digit number code. I have tried different things with the delimiters, such as spaces and the second indentifiers, but it always either leaves something out or adds too much; plus it always has the number code and I do not want that. Is there maybe a way to set it to write only the text within the 5-33 positions and ignore fields that have no text in these positions? I need to load the list dynamically because the blank fields will not always be blank and the screens I am working on will need to reflect the changes. I know that there is way to do this with COBOL using multi editor because that is how the program I am working on was created originally, but I am supposed to be designing a more modern GUI and would really like to accomplish this with VB. Any help would be great.
    Attached Files Attached Files

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Dynamically populate listview from file?

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim items() As ListViewItem = (From line In IO.File.ReadAllLines("workfile[1].txt") _
    5.                                 Where line.Trim <> "" AndAlso line.Substring(4, 29).Trim <> "" _
    6.                                 Select New ListViewItem(line.Substring(4, 29).Trim)).ToArray
    7.         ListView1.Items.AddRange(items)
    8.     End Sub
    9.  
    10. End Class

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Apr 2011
    Posts
    16

    Re: Dynamically populate listview from file?

    Thanks a lot Paul. That was exactly what I was looking for and besides it working better than my code it was a lot shorter too. I gave you a good rating. Thanks again.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: [RESOLVED] Dynamically populate listview from file?

    ok thanks. it's always good to add another rating to my collection

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width