Help needed with loading a text file into a listbox (VB.net)

Discussion in 'Code Snippets and Tutorials' started by test123, Feb 17, 2007.

  1. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    what im trying to do is load a list into a listbox..the dialog works fine, i can search for a .txt file i want but after i open it, it doesn't load into my listbox

    here is my open button code

    Code (Text):
    1.  
    2. Private Sub openbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    3.  
    4.         OpenFileDialog1.Title = "open"
    5.         OpenFileDialog1.Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
    6.         OpenFileDialog1.FileName = ""
    7.         OpenFileDialog1.FilterIndex = 0
    8.  
    9.  
    10.         OpenFileDialog1.InitialDirectory = "MyDocuments"
    11.         OpenFileDialog1.CheckFileExists = True
    12.         OpenFileDialog1.CheckPathExists = True
    13.  
    14.         If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    15.  
    16.             ListBox1.Items.Contains(OpenFileDialog1.FileName)
    17.  
    18.         End If
    19.  
    20.     End Sub
    anyone have an idea why its not working?
     
  2. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    hey

    yea "OpenFileDialog1" is my dialog component
     
  3. expon

    expon Administrator
    Staff Member

    Joined:
    Oct 30, 2006
    Messages:
    1,393
    Likes Received:
    56
    Location:
    UK
    Re: Help needed with loading a text file into a listbox (VB.

    Code (Text):
    1.  
    2. Private Sub openbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    3.  
    4.         OpenFileDialog1.Title = "open"
    5.         OpenFileDialog1.Filter = "Text Documents (*.txt)|*.txt|All Files (*.*)|*.*"
    6.         OpenFileDialog1.FileName = ""
    7.         OpenFileDialog1.FilterIndex = 0
    8.  
    9.  
    10.         OpenFileDialog1.InitialDirectory = "MyDocuments"
    11.         OpenFileDialog1.CheckFileExists = True
    12.         OpenFileDialog1.CheckPathExists = True
    13.  
    14.         If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    15.             Dim aline As String
    16.  
    17.             Dim oRead As System.IO.StreamReader
    18.             oRead = File.OpenText(OpenFileDialog1.FileName)
    19.  
    20.             While oRead.Peek <> -1
    21.  
    22.                 aline = oRead.ReadLine()
    23.                listbox1.Items.Add(aline)
    24.  
    25.             End While
    26.  
    27.         Else
    28.             MsgBox("Error Loading File")
    29.         End If
    30.  
    31.     End Sub
    try that

    oh and you've imported System.IO right?
     
  4. test123

    test123 Level IV

    Joined:
    Nov 3, 2006
    Messages:
    935
    Likes Received:
    0
    Location:
    Australia
    hey

    worked :D thanks heaps expon