【问题标题】:C# - Saving values in ListboxC# - 在列表框中保存值
【发布时间】:2011-07-13 16:53:03
【问题描述】:

我有以下代码:

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”下有一个red 行。我以前曾使用此代码保存文件,但不确定为什么它不适用于 ListBox。


问题

  • 如何以与 RichTextBox 类似的方式保存 ListBox?
  • 有什么方法可以编辑此代码,让用户选择 ListBox 的保存位置?

【问题讨论】:

    标签: c# listbox save text-files savefiledialog


    【解决方案1】:

    ListBox 不提供将其所有项目保存到文件的方法。代码 sn-p 显示如何做到这一点可在此处获得:

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

    【讨论】:

      猜你喜欢
      • 2014-08-26
      • 1970-01-01
      • 2013-06-10
      • 1970-01-01
      • 2011-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多