Page 1 of 2

Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 9:02 pm
by GoodGuy17
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:
Code: Select all
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
Save Playlist:
Code: Select all
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
Add Song:
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 :D

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 9:57 pm
by mandai
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))

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:01 pm
by GoodGuy17
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?

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:17 pm
by mandai
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

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:20 pm
by GoodGuy17
What is file?

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:23 pm
by mandai
The System.IO.File class.

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:25 pm
by GoodGuy17
And how do I use this code altogether? What do I edit and what do I edit it to?

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:31 pm
by mandai
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.

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:33 pm
by GoodGuy17
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?

Re: Reading a file and populating a listbox

Posted: Thu Aug 19, 2010 10:43 pm
by mandai
I used that code by itself and it works fine, it doesen't seem obvious what may be causing the problem.