【问题标题】:How to keep "Save" and "Save as..." buttons work at the same time如何保持“保存”和“另存为...”按钮同时工作
【发布时间】:2018-01-09 23:11:35
【问题描述】:

我有一个带有 3 个按钮的程序:“加载”、“保存”和“另存为”。 在您“加载”文件(或“另存为”更好版本的文件)之前,“保存”按钮最初是灰色的。

“加载”使用 OpenFileDialog,“另存为”使用 SaveFileDialog,而“保存”重用 openFileDialog.FileName 属性作为其 StreamWriter 的参数。

问题是,我可以“加载”->“保存”一个文件,但我不能“加载”->“保存”->“另存为”,因为“保存”方法总是使用 openFileDialog1.FileName ,但当前使用的文件是 saveAsFileDialog.FileName,所以它会一直保存在前一个文件上。

Tl;dr:我需要创建一个“字符串 currentFileName”,它会在每次“加载”和“另存为”操作后更新,以便“保存”方法知道最新文件是否是“加载”文件或“另存为”之一。问题是我不知道如何在我的 Form.cs 中创建一个变量,该变量不仅可以在主 Form 方法中看到,还可以在每个控件的事件处理程序生成的方法中看到。我认为我需要一个全局变量之类的东西。

编辑:添加代码。

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(openFileDialog1.FileName))
                    {
                        string inputLine;
                        while ((inputLine = sr.ReadLine()) != null)
                        {
                            // some operations
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("The file could not be read:");
                    Console.WriteLine(ex.Message);
                }
            }

            // Enable "Save" Button
            saveToolStripMenuItem.Enabled = true;

            // currentFileName = openFileDialog1.FileName
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))
                {
                    // some operations
                }
            }

            // currentFileName = saveFileDialog1.FileName
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // openFileDialog1.FileName should be replaced by currentFileName
            using (StreamWriter sw = new StreamWriter(openFileDialog1.FileName))
            {
                // some operations
            }
        }
    }
}

【问题讨论】:

  • 我假设在您的表单中,您处理触发保存方法的事件。你确实需要一个类变量。也许向我们展示您已经拥有的代码?
  • 使用类级别变量来保存文件的当前名称。此外,您可能应该在表单的某个位置向您的客户展示它。
  • 您可以根据需要添加任意数量的对话框(假设这是 Windows 窗体) - 即 1 个打开和 2 个保存。
  • 我添加了代码。我需要知道如何创建 currentOpenedFile 变量,以便我可以在任何我想要的地方使用它,所以如果有人能告诉我在哪里准确放置构造函数指令......

标签: c# save


【解决方案1】:

尝试这样的事情(请不要忘记添加第二个 saveDialog):

public partial class Form1 : Form
{
String pathName;
    public Form1()
    {
        InitializeComponent();
    }

    private void loadToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
    pathname = openFileDialog1.FileName;
            try
            {
                using (StreamReader sr = new StreamReader(pathname))
                {
                    string inputLine;
                    while ((inputLine = sr.ReadLine()) != null)
                    {
                        // some operations
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(ex.Message);
            }
        }

        // Enable "Save" Button
        saveToolStripMenuItem.Enabled = true;

        // currentOpenedFile.FileName = openFileDialog1.FileName
    }

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
    {
        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
    pathname = saveFileDialog2.FileName;
            using (StreamWriter sw = new StreamWriter(pathname))
            {
                // some operations
            }
        }

        // currentOpenedFile.FileName = saveFileDialog1.FileName
    }

    private void saveToolStripMenuItem_Click(object sender, EventArgs e)
    {
        // openFileDialog1.FileName should be replaced by currentOpenedFile.FileName
        using (StreamWriter sw = new StreamWriter(pathname))
        {
            // some operations
        }
    }
}

}

【讨论】:

  • "把 pathName 字符串放在 "public partial class Form1 : Form"" 之后,我只需要知道,谢谢。
猜你喜欢
  • 1970-01-01
  • 2022-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多