Adding .txt File into Listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Maxisking
    New Member
    • Oct 2007
    • 8

    #1

    Adding .txt File into Listbox

    Well, Im wondering how i can do this:
    I have a load button and a listbox.

    When i click The Load Button you select a .txt file to load into the Listbox, Thanks :)

    VB.NET Btw
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    What does the textfile contains ?

    Why you need to add the containt of txt file to listbox it can be easily loaded to any textbox.

    Comment

    • balabaster
      Recognized Expert Contributor
      • Mar 2007
      • 798

      #3
      Originally posted by Maxisking
      Well, Im wondering how i can do this:
      I have a load button and a listbox.

      When i click The Load Button you select a .txt file to load into the Listbox, Thanks :)

      VB.NET Btw
      I'm assuming that you are going to be holding the contents of the list box in the text file so that you can modify this without modifying your code and recompiling. It really depends on the format of your file...for instance, is it XML, plaintext or some other format. Assuming the file is just plain text and that the file holds one listbox entry per line:

      Code:
      Dim oText As System.IO.StreamReader = Nothing
      Try
          oText = System.IO.File.OpenText("C:\MyList.txt")
          While Not oText.EndOfStream
              Dim sCurrentLine As String = oText.ReadLine
              ListBox1.Items.Add(sCurrentLine)
          End While
      Catch ex As Exception
          MsgBox(ex.Message)
      Finally
          If Not oText Is Nothing Then
              oText.Close()
              oText.Dispose()
          End If
      End Try
      Obviously you wouldn't hard code the name of the file as that's pretty bad coding practice...you' d likely load the name from a settings file, but that should give you an idea of how to load the content of a text file into a list box.

      Comment

      Working...