2017-05-28 5 views
0

私は初めて自分でコードを作成しようとしており、Winformsで音楽プレーヤーを作ることに決めました。CheckedListBox選択をテキストファイルに送る方法は?

私はすでにフォルダ内の曲の名前が移入さにCheckedListBoxを持っています。 ボタンをクリックすると、フォームを閉じる前に、選択した曲の名前を.txtファイルに送信してさらに操作できます。簡略化のため

、私は最初の1つの選択された歌で行きますよ。私streamreader/writerクラスの

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(); 
} 

、これは私がそれを実行すると、.txtファイルは私の選択が表示されない

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(); 
     } 
    } 
} 

を持っているものです。それだけを示しています。 System.Windows.Forms.CheckedListBox+CheckedItemCollection

を何が悪かったのか?

+0

[設定]を保存するにCheckedListBox項目](https://stackoverflow.com/q/32773280/3110834) –

答えて

1

CheckedItemsコレクションを反復処理し、文字列配列内の各アイテムを集めます。私はあなたが文字列とにCheckedListBoxを埋める仮定

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(); 

    } 
関連する問題