【问题标题】:Adding a Save As Function to a form using a listbox store of data使用数据的列表框存储向表单添加另存为函数
【发布时间】:2014-12-17 15:01:43
【问题描述】:

我正在用 C# 在 Visual Studio Express 2014 中创建一个时间表表单。

我已经创建了整个表单,它可以充分发挥作用,甚至可以保存到文本文件中,但是我希望添加一个Savedialog 以便用户选择保存文件的位置,就像保存在 word 中一样等。

我尝试过使用File.WriteAllText(name, "test"); 或其变体,但这不适用于listbox

在我的代码中,listbox 被称为结果, 这是我迄今为止尝试过的保存按钮代码:

private void save_Click(object sender, EventArgs e)
{
    const string Path = "C:\\Users\\Loan\\save.txt";
    System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(Path);
    foreach (var item in results.Items)
    {
        SaveFile.WriteLine(item.ToString());
    }
    SaveFile.Close();

    MessageBox.Show("Programs saved!");

}

它工作得很好,但是正如我前面提到的那样

我希望创建一个另存为功能供用户浏览到哪里 保存。

表格 = http://gyazo.com/227d2aada349586cef60f2962456a71c

完整代码 = http://pastebin.com/7DwLpkhP

【问题讨论】:

    标签: c# .net winforms listbox


    【解决方案1】:

    使用`Save file dialog'

    using (SaveFileDialog dialog = new SaveFileDialog())
    {
        dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
        dialog.FilterIndex = 2 ;
        dialog.RestoreDirectory = true ;
    
        if (dialog.ShowDialog() == DialogResult.OK)
        {
            // Can use dialog.FileName
            string Path = dialog.FileName;
           ....
    

    【讨论】:

      猜你喜欢
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多