2011-07-13 45 views
4

我有以下代碼:C# - 在列表框保存價值

SaveFileDialog saveGC1File = new SaveFileDialog(); 

private void GCSavePlacementOneButton_Click(object sender, EventArgs e) 
{ 
    // Initialize the SaveFileDialog to specify the .txt extension for the file. 
    saveGC1File.DefaultExt = "*.txt"; 
    saveGC1File.Filter = ".txt Files|*.txt|All Files (*.*)|*.*"; 
    saveGC1File.RestoreDirectory = true; 

    try 
    { 
     string placementOneSave = placementOneListBox.Items.ToString(); 

     // Save the contents of the formattedTextRichTextBox into the file. 
     if (saveGC1File.ShowDialog() == DialogResult.OK && saveGC1File.FileName.Length > 0) 
      placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText); 

     // Throws a FileNotFoundException otherwise. 
     else 
      throw new FileNotFoundException(); 
    } 

    // Catches an exception if the file was not saved. 
    catch (Exception) 
    { 
     MessageBox.Show("There was not a specified file path.", "Path Not Found Error", 
         MessageBoxButtons.OK, MessageBoxIcon.Warning); 
    } 
} 

然而,使用Visual Studio 2010年,在 「如果」 環行,指出:

"placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);" 

擁有根據「SAVEFILE」紅行..我已經使用這個代碼來保存前一個文件,我不能確定爲什麼它不會爲一個ListBox工作。


質詢

  • 我怎麼能以類似的方式列表框保存到一個RichTextBox?
  • 任何方式修改這個代碼,使其讓用戶選擇在列表框將被保存?

回答