VB.net list help

Discussion in 'World of SPAM' started by Fexxel, Jun 15, 2009.

  1. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Hey (Tharoux)! (lol)

    Quick VB.net question...
    What's the code for loading the contents of a .txt file into a listbox?
    Thanks :)

    PS:
    I have my money betting that Tharoux will save the day first! XD
     
  2. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Code (Text):
    1.     Public Sub LoadList(ByRef lstList As ListBox)
    2.         Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String
    3.         Dim OD As New OpenFileDialog
    4.         OD.FileName = ""
    5.         OD.Multiselect = False
    6.         OD.Title = "Open List File"
    7.         OD.AddExtension = True
    8.         OD.Filter = "Text files (*.txt)|*.txt"
    9.         OD.DefaultExt = "txt"
    10.         OD.ShowDialog()
    11.         If OD.FileName = "" Or OD.CheckFileExists() = False Then Exit Sub
    12.         objStream = New StreamReader(OD.FileName, True)
    13.         objStream = File.OpenText(OD.FileName)
    14.         Do While objStream.EndOfStream = False
    15.             buffer = objStream.ReadToEnd.ToString
    16.         Loop
    17.         arrFile = Regex.Split(buffer, vbCrLf)
    18.         For i As Integer = 0 To UBound(arrFile)
    19.             lstList.Items.Add(arrFile(i))
    20.         Next
    21.     End Sub
    22.     Public Sub LoadList(ByVal strPath As String, ByRef lstList As ListBox)
    23.         Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String
    24.         objStream = New StreamReader(strPath, True)
    25.         objStream = File.OpenText(strPath)
    26.         Do While objStream.EndOfStream = False
    27.             buffer = objStream.ReadToEnd.ToString
    28.         Loop
    29.         arrFile = Regex.Split(buffer, vbCrLf)
    30.         For i As Integer = 0 To UBound(arrFile)
    31.             lstList.Items.Add(arrFile(i))
    32.         Next
    33.     End Sub
    You can use either. The first one automatically creates an openfiledialog, while the second lets you choose the path of the file to load. You can keep both in your code as Vb.net has support for multiple definitions (if they have a different print - different parameters and/or return values).
     
  3. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Whoa, Ricky92 to the rescue!
    ----
    Question time.
    Prepare yourself for pure noob-ness
    1.
    Code (Text):
    1. Error   Argument not specified for parameter 'pages' of 'Public Sub LoadList(ByRef pages As System.Windows.Forms.ListBox)'.
    2.  
    2.
    Code (Text):
    1.     Public Sub LoadList(ByRef pages As ListBox)
    2.         Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String
    3.         Dim OD As New OpenFileDialog
    4.         OD.FileName = ""
    5.         OD.Multiselect = False
    6.         OD.Title = "Open List File"
    7.         OD.AddExtension = True
    8.         OD.Filter = "Text files (*.txt)|*.txt"
    9.         OD.DefaultExt = "txt"
    10.         OD.ShowDialog()
    11.         If OD.FileName = "" Or OD.CheckFileExists() = False Then Exit Sub
    12.         objStream = New StreamReader(OD.FileName, True)
    13.         objStream = File.OpenText(OD.FileName)
    14.         Do While objStream.EndOfStream = False
    15.             buffer = objStream.ReadToEnd.ToString
    16.         Loop
    17.         arrFile = Regex.Split(buffer, vbCrLf)
    18.         For i As Integer = 0 To UBound(arrFile)
    19.             pages.Items.Add(arrFile(i)) <---- [b]So this is where the name of the listbox goes?[/b]
    20.         Next
    21.     End Sub
    3. If I were to add this to a loadlist button:

    Code (Text):
    1.     Private Sub EXAMPLEBUTTONNAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         Dim w As New TCPWrapper
    3.         w.LoadList()
    4.     End Sub/code] (If I had added LoadList to TCPWrapper function
    5.  
    6. And if I had not..
    7. [code]    Private Sub EXAMPLEBUTTONNAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    8.  LoadList()
    9.     End Sub
    Yet they both return error 1 (see question #1)?
     
  4. tharoux

    tharoux Level IV

    Joined:
    Dec 30, 2006
    Messages:
    2,733
    Likes Received:
    126
    Location:
    In front of my PC, Montreal
    Damn you Ricky !!!! :D
    Here's mine... not a separate function...
    list name is lstItems. The dialog with show txt by default and *.* if wanted.
    And by default, it will automaticaly open the folder where the executable is running.

    Code (Text):
    1. Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click
    2.         Dim OpenFileDialog1 As New OpenFileDialog()
    3.         Dim Line As String
    4.         OpenFileDialog1.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory()
    5.         OpenFileDialog1.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt"
    6.         OpenFileDialog1.FilterIndex = 2
    7.         OpenFileDialog1.RestoreDirectory = True
    8.         If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
    9.             Dim myStream As StreamReader
    10.             Try
    11.                 myStream = New StreamReader(OpenFileDialog1.FileName)
    12.                 If (myStream IsNot Nothing) Then
    13.                     Line = myStream.ReadLine
    14.                     Do While (Not Line Is Nothing)
    15.                         lstItems.Items.Add(Line)
    16.                         Line = myStream.ReadLine
    17.                         lstItems.SelectedIndex = 0
    18.                     Loop
    19.                     UpdateNumberOfItems()
    20.                     myStream.Close()
    21.                 End If
    22.             Catch Ex As Exception
    23.                 MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message)
    24.             End Try
    25.         End If
    26.     End Sub
     
  5. Fexxel

    Fexxel Level IV

    Joined:
    Jan 28, 2009
    Messages:
    959
    Likes Received:
    26
    Gah too late!
    Oh snap!
    Ricky92 got here first ^^
     
  6. ricky92

    ricky92 Administrator
    Staff Member

    Joined:
    Nov 10, 2006
    Messages:
    1,866
    Likes Received:
    67
    Code (Text):
    1. LoadList(NameOfYourListBoxHere)
    OR

    Code (Text):
    1. LoadList(PathOfTheFileToLoad, NameOfListBoxHere)
     
    Fexxel likes this.