4

次のコードがあります。

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 を使用すると、"if" ループの行は次のようになります。

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

「 SaveFile 」の下に赤い線があります..以前にこのコードを使用してファイルを保存しましたが、ListBox で機能しない理由がわかりません。


質問

  • RichTextBox と同様の方法で ListBox を保存するにはどうすればよいですか?
  • このコードを編集して、ユーザーが ListBox を保存する場所を選択できるようにする方法はありますか?
4

1 に答える 1

2

ListBox には、すべての項目をファイルに保存する方法はありません。これを行う方法を示すコード スニペットは、次の場所にあります。

リストボックスのテキストをテキスト ファイルに保存する C# コード -- 解決済み --

于 2011-07-13T17:00:05.600 に答える