C # - Saving Values ​​in Listbox

I have the following code:

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

However, using Visual Studio 2010, the line in the "if" loop says:

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

Has a string red under "SaveFile". I used this code to save the file before and I don’t know why it will not work for the ListBox.


QUESTIONS

  • How to save ListBox in the same way as RichTextBox?
  • Any way to edit this code so that the user can choose where the ListBox will be stored?
+4
source share
1 answer

ListBox does not provide a way to save all its elements in a file. The following is a code snippet showing how this can be done:

C # code to save text from a list to a text file - SOLVED -

+2
source

All Articles