Loading text files into a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peterhinett
    New Member
    • Nov 2007
    • 8

    #1

    Loading text files into a listbox

    I'm trying to add add text files to a listbox. I have got the directory box to come up but then when I click add it adds the whole file like

    d:/ readings.txt but I want it to display all the readings within the file. This is my code so far.

    [CODE=vbnet]Private Sub btnload_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnload.Click
    openFileDialog1 .InitialDirecto ry = "d:\"
    openFileDialog1 .RestoreDirecto ry = True
    openFileDialog1 .Multiselect = True
    If openFileDialog1 .ShowDialog() = DialogResult.OK Then
    For Each iName In openFileDialog1 .FileNames
    lbreading.Items .Add(iname)
    Next
    End If
    End Sub[/CODE]
    Last edited by Killer42; Nov 19 '07, 02:33 AM. Reason: Added CODE=vbnet tag
  • user232k2038
    New Member
    • Sep 2007
    • 16

    #2
    You mean you want each line in the file to be an element in the listbox? If so...

    [CODE=vbnet]Private Sub btnload_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnload.Click

    openFileDialog1 .InitialDirecto ry = "d:\"
    openFileDialog1 .RestoreDirecto ry = True
    openFileDialog1 .Multiselect = True
    If openFileDialog1 .ShowDialog() = DialogResult.OK Then
    For Each iName In openFileDialog1 .FileNames
    Dim sread As New IO.StreamReader (iName), s As String() = sread.ReadToEnd ().Split(Chr(10 ))
    For Each str As String In s
    MsgBox(str)
    Next
    Next
    End If
    End Sub[/CODE]
    Last edited by Killer42; Nov 19 '07, 02:36 AM.

    Comment

    • peterhinett
      New Member
      • Nov 2007
      • 8

      #3
      That has just brought each one up in a message box. do you know how to get it so they all appear in the listbox??


      Originally posted by aaaaaaaaaaaaaaa aaaaaaaaaa
      You mean you want each line in the file to be an element in the listbox? If so...

      [CODE=vbnet]Private Sub btnload_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles btnload.Click

      openFileDialog1 .InitialDirecto ry = "d:\"
      openFileDialog1 .RestoreDirecto ry = True
      openFileDialog1 .Multiselect = True
      If openFileDialog1 .ShowDialog() = DialogResult.OK Then
      For Each iName In openFileDialog1 .FileNames
      Dim sread As New IO.StreamReader (iName), s As String() = sread.ReadToEnd ().Split(Chr(10 ))
      For Each str As String In s
      MsgBox(str)
      Next
      Next
      End If
      End Sub[/CODE]

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by peterhinett
        That has just brought each one up in a message box. do you know how to get it so they all appear in the listbox??
        Use the Add like in your previous message, in place of the MsgBox.

        Comment

        Working...