微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C# – 在Listbox中保存值

我有以下代码

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的保存位置?

解决方法

ListBox不提供将其所有项目保存到文件方法.此处提供了显示如何完成此操作的代码段:

C# code to save text from listbox to a text file — SOLVED–

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐