Reading a file and populating a listbox
Do you need something made? then ask in here.
Forum rules
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Please LOCK your topics once you have found the solution to your question so we know you no longer require help with your query.
Hello,
I am making a music program that has a custom file extension(mspf), and here I go.
I am trying to add songs to a playlist, and save the playlist. When I open the playlist I saved, the listbox says -1. That is it. When I save the playlist, it saves the directories of the songs. When I open the playlist, I want it to show the things I saved. Here is my code:
Open Playlist:
Private Sub AddSongToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSongToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Add Song"
OFD.FileName = ""
OFD.Filter = "MP3 Files(*.mp3)|*.mp3|WAV Files(*.wav)|*.wav"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.Text = RichTextBox1.Text + vbNewLine & OFD.FileName
Music.Items.Remove(Music.Items.Count)
Music.Items.Add(RichTextBox1.Text)
End If
End Sub
Please help, this is a project I am working on, and I am trying to get it done. +rep to whoever answers first! cooll;
~GoodGuy17
I am making a music program that has a custom file extension(mspf), and here I go.
I am trying to add songs to a playlist, and save the playlist. When I open the playlist I saved, the listbox says -1. That is it. When I save the playlist, it saves the directories of the songs. When I open the playlist, I want it to show the things I saved. Here is my code:
Open Playlist:
Code: Select all
Save Playlist:
Private Sub OpenPlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPlaylistToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
OFD.FileName = ""
OFD.Title = "Open Playlist"
If OFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Reader As New System.IO.StreamReader(OFD.FileName)
RichTextBox1.Text = Reader.Read
Music.Items.Add(RichTextBox1.Text)
ToolStripStatusLabel1.Text = "Opened: " & OFD.FileName
End If
End Sub
Code: Select all
Add Song:Private Sub SavePlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePlaylistToolStripMenuItem.Click
Dim SFD As New SaveFileDialog
SFD.Title = "Save Playlist"
SFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
SFD.FileName = ""
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Writer As New System.IO.StreamWriter(SFD.FileName)
For i As Integer = 0 To RichTextBox1.Lines.Count
Writer.WriteLine(RichTextBox1.Text)
Next
End If
End Sub
Private Sub AddSongToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSongToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Add Song"
OFD.FileName = ""
OFD.Filter = "MP3 Files(*.mp3)|*.mp3|WAV Files(*.wav)|*.wav"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.Text = RichTextBox1.Text + vbNewLine & OFD.FileName
Music.Items.Remove(Music.Items.Count)
Music.Items.Add(RichTextBox1.Text)
End If
End Sub
Please help, this is a project I am working on, and I am trying to get it done. +rep to whoever answers first! cooll;
~GoodGuy17

Looks like your save code will keep repeating the same text of the richtextbox for each line. Try
Code: Select all
Writer.WriteLine(RichTextBox1.Lines(i))
Now it shows System.String[] in the listbox.
What do I do to this code:
Private Sub OpenPlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPlaylistToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
OFD.FileName = ""
OFD.Title = "Open Playlist"
If OFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Reader As New System.IO.StreamReader(OFD.FileName)
RichTextBox1.Text = Reader.Read
Music.Items.Add(RichTextBox1.Lines.ToString)
ToolStripStatusLabel1.Text = "Opened: " & OFD.FileName
End If
End Sub
Private Sub SavePlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePlaylistToolStripMenuItem.Click
Dim SFD As New SaveFileDialog
SFD.Title = "Save Playlist"
SFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
SFD.FileName = ""
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Writer As New System.IO.StreamWriter(SFD.FileName)
For i As Integer = 0 To RichTextBox1.Lines.Count
Writer.WriteLine(RichTextBox1.Text(i))
Next
End If
End Sub
Private Sub AddSongToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSongToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Add Song"
OFD.FileName = ""
OFD.Filter = "MP3 Files(*.mp3)|*.mp3|WAV Files(*.wav)|*.wav"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.Text = RichTextBox1.Text + vbNewLine & OFD.FileName
Music.Items.Remove(Music.Items.Count)
Music.Items.Add(RichTextBox1.Text)
End If
End Sub
What do I modify to work?
What do I do to this code:
Private Sub OpenPlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenPlaylistToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
OFD.FileName = ""
OFD.Title = "Open Playlist"
If OFD.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim Reader As New System.IO.StreamReader(OFD.FileName)
RichTextBox1.Text = Reader.Read
Music.Items.Add(RichTextBox1.Lines.ToString)
ToolStripStatusLabel1.Text = "Opened: " & OFD.FileName
End If
End Sub
Private Sub SavePlaylistToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SavePlaylistToolStripMenuItem.Click
Dim SFD As New SaveFileDialog
SFD.Title = "Save Playlist"
SFD.Filter = "Music Studio Playlist Files(*.mspf)|*.mspf"
SFD.FileName = ""
If SFD.ShowDialog = Windows.Forms.DialogResult.OK Then
Dim Writer As New System.IO.StreamWriter(SFD.FileName)
For i As Integer = 0 To RichTextBox1.Lines.Count
Writer.WriteLine(RichTextBox1.Text(i))
Next
End If
End Sub
Private Sub AddSongToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddSongToolStripMenuItem.Click
Dim OFD As New OpenFileDialog
OFD.Title = "Add Song"
OFD.FileName = ""
OFD.Filter = "MP3 Files(*.mp3)|*.mp3|WAV Files(*.wav)|*.wav"
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
RichTextBox1.Text = RichTextBox1.Text + vbNewLine & OFD.FileName
Music.Items.Remove(Music.Items.Count)
Music.Items.Add(RichTextBox1.Text)
End If
End Sub
What do I modify to work?
This is how I'd do it
Code: Select all
Sub loadPlaylist(ByVal filename As String)
Dim lines As String() = File.ReadAllLines(filename)
For i As Integer = 0 To lines.Length - 1
ListBox1.Items.Add(lines(i))
Next
End Sub
Sub savePlaylist(ByVal filename As String)
Dim tosave As String = ""
For i As Integer = 0 To ListBox1.Items.Count - 1
tosave += ListBox1.Items(i).ToString() & vbCrLf
Next
File.WriteAllText(filename, tosave)
End Sub
Sub addSong(ByVal songName As String, ByVal filename As String)
File.AppendAllText(filename, songName & vbCrLf)
loadPlaylist(filename)
End Sub
'extra
Sub removeSongAt(ByVal index As Integer, ByVal filename As String)
ListBox1.Items.Remove(index)
savePlaylist(filename)
End Sub
Last edited by mandai on Thu Aug 19, 2010 10:23 pm, edited 1 time in total.
And how do I use this code altogether? What do I edit and what do I edit it to?
Just put it in your class then use loadPlaylist in your OpenPlaylistToolStripMenuItem_Click subroutine, savePlaylist in the SavePlaylistToolStripMenuItem_Click subroutine, addSong in the AddSongToolStripMenuItem_Click subroutine.
Ok, I did so. When I use this, I find a song and open it to add to playlist, and the listbox like scrolls SUPERFAST and the bar becomes a cm tall in like 3 seconds. Should I PM you the source and let you work it out?
I used that code by itself and it works fine, it doesen't seem obvious what may be causing the problem.
Copyright Information
Copyright © Codenstuff.com 2020 - 2023