4

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 that states:

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

Has a red line under the "SaveFile".. I have used this code to save a file before and am unsure why it will not work for a ListBox.


QUESTIONS

  • How can I save a ListBox in a similar way to a RichTextBox?
  • Any way to edit this code to make it have the user choose where the ListBox will be saved?

1 Answer 1

2

ListBox does not provide a method to save all its items to a file. The code snippet showing how this can be done is available here:

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.