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
Code (Text): Public Sub LoadList(ByRef lstList As ListBox) Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String Dim OD As New OpenFileDialog OD.FileName = "" OD.Multiselect = False OD.Title = "Open List File" OD.AddExtension = True OD.Filter = "Text files (*.txt)|*.txt" OD.DefaultExt = "txt" OD.ShowDialog() If OD.FileName = "" Or OD.CheckFileExists() = False Then Exit Sub objStream = New StreamReader(OD.FileName, True) objStream = File.OpenText(OD.FileName) Do While objStream.EndOfStream = False buffer = objStream.ReadToEnd.ToString Loop arrFile = Regex.Split(buffer, vbCrLf) For i As Integer = 0 To UBound(arrFile) lstList.Items.Add(arrFile(i)) Next End Sub Public Sub LoadList(ByVal strPath As String, ByRef lstList As ListBox) Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String objStream = New StreamReader(strPath, True) objStream = File.OpenText(strPath) Do While objStream.EndOfStream = False buffer = objStream.ReadToEnd.ToString Loop arrFile = Regex.Split(buffer, vbCrLf) For i As Integer = 0 To UBound(arrFile) lstList.Items.Add(arrFile(i)) Next 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).
Whoa, Ricky92 to the rescue! ---- Question time. Prepare yourself for pure noob-ness 1. Code (Text): Error Argument not specified for parameter 'pages' of 'Public Sub LoadList(ByRef pages As System.Windows.Forms.ListBox)'. 2. Code (Text): Public Sub LoadList(ByRef pages As ListBox) Dim objStream As StreamReader, buffer As String = String.Empty, arrFile() As String Dim OD As New OpenFileDialog OD.FileName = "" OD.Multiselect = False OD.Title = "Open List File" OD.AddExtension = True OD.Filter = "Text files (*.txt)|*.txt" OD.DefaultExt = "txt" OD.ShowDialog() If OD.FileName = "" Or OD.CheckFileExists() = False Then Exit Sub objStream = New StreamReader(OD.FileName, True) objStream = File.OpenText(OD.FileName) Do While objStream.EndOfStream = False buffer = objStream.ReadToEnd.ToString Loop arrFile = Regex.Split(buffer, vbCrLf) For i As Integer = 0 To UBound(arrFile) pages.Items.Add(arrFile(i)) <---- [b]So this is where the name of the listbox goes?[/b] Next End Sub 3. If I were to add this to a loadlist button: Code (Text): Private Sub EXAMPLEBUTTONNAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim w As New TCPWrapper w.LoadList() End Sub/code] (If I had added LoadList to TCPWrapper function And if I had not.. [code] Private Sub EXAMPLEBUTTONNAME_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click LoadList() End Sub Yet they both return error 1 (see question #1)?
Damn you Ricky !!!! 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): Private Sub cmdLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoad.Click Dim OpenFileDialog1 As New OpenFileDialog() Dim Line As String OpenFileDialog1.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory() OpenFileDialog1.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt" OpenFileDialog1.FilterIndex = 2 OpenFileDialog1.RestoreDirectory = True If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then Dim myStream As StreamReader Try myStream = New StreamReader(OpenFileDialog1.FileName) If (myStream IsNot Nothing) Then Line = myStream.ReadLine Do While (Not Line Is Nothing) lstItems.Items.Add(Line) Line = myStream.ReadLine lstItems.SelectedIndex = 0 Loop UpdateNumberOfItems() myStream.Close() End If Catch Ex As Exception MessageBox.Show("Cannot read file from disk. Original error: " & Ex.Message) End Try End If End Sub
Code (Text): LoadList(NameOfYourListBoxHere) OR Code (Text): LoadList(PathOfTheFileToLoad, NameOfListBoxHere)