加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C# – 在Listbox中保存值

发布时间:2020-12-15 17:18:29 所属栏目:百科 来源:网络整理
导读:我有以下代码: 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 =
我有以下代码:

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–

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读