Search code examples
c#winformstext-filesstreamwritercheckedlistbox

How to send CheckedListBox selection into textfile?


I'm trying to code on my own for the first time, and decided to make a music player on Winforms.

I have a CheckedListBox that is already populated with the names of songs in a folder. When I click a button, it's supposed to send the names of my selected songs into a .txt file for further manipulation, before closing the form.

For simplification, I'm just going with 1 selected song first.

private void selectbtn_Click(object sender, EventArgs e)
{
    selectedSongs = checkedListBox1.CheckedItems.ToString();
    songRecord.writeRecord(selectedSongs); //i initialised my streamreader/streamwriter class and called it songRecord
    this.Close();   
}

in my streamreader/writer class, this is what I have

class DataRecord
{
    public void writeRecord(string line)
    {
        StreamWriter sw = null;
        try
        {
            sw = new StreamWriter(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt", true);
            sw.WriteLine(line);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Error: File not found.");
        }
        catch (IOException)
        {
            Console.WriteLine("Error: IO");
        }
        catch(Exception)
        {
            throw;
        }
        finally
        {
            if (sw != null)
                sw.Close();
        }
    }

    public void readRecord()
    {
        StreamReader sr = null;
        string myInputline;
        try
        {
            sr = new StreamReader(@"C:\Users\Me\Desktop\JAM_MACHINE\record.txt");
            while ((myInputline = sr.ReadLine()) != null) ; //readline reads whole line
            Console.WriteLine(myInputline);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine("Error: File not found");
        }
        catch(IOException)
        {
            Console.WriteLine("Error: IO");
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (sr != null)
                sr.Close();
        }
    }
}

When I run it, the .txt file doesn't show my selection. It only shows: System.Windows.Forms.CheckedListBox+CheckedItemCollection

What went wrong?


Solution

  • Iterate through the CheckedItems Collection and collect each item inside a string array. I assume you fill the checkedListBox with strings

       private void selectbtn_Click(object sender, EventArgs e)
        {
    
    
            string[] checkedtitles = new string[checkedListBox1.CheckedItems.Count];
            for (int ii = 0; ii < checkedListBox1.CheckedItems.Count; ii++)
            {
                checkedtitles[ii] = checkedListBox1.CheckedItems[ii].ToString();
            }
            string selectedSongs = String.Join(Environment.NewLine, checkedtitles);
    
            songRecord.writeRecord(selectedSongs);
            this.Close();
    
        }