gpt4 book ai didi

c# - 关闭保存文件对话框

转载 作者:行者123 更新时间:2023-12-03 20:16:04 26 4
gpt4 key购买 nike

我在关闭 saveFileDialog 时遇到问题。当我点击“取消”窗口时再次出现。这是我的代码:

        private void SaveAsItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "untitled";
saveFileDialog1.Filter = "Text (*.txt)|*.txt";
saveFileDialog1.ShowDialog();
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
if (saveFileDialog1.ShowDialog()==DialogResult.Cancel)
{
richTextBox1.Text = "CANCEL";
issaved = false;
}
else
{
issaved = true;
SaveFile.WriteLine(richTextBox1.Text);
}
SaveFile.Close();
}

最佳答案

您正在调用 saveFileDialog1.ShowDialog() 两次,一次显示它,一次获取结果。你只需要调用一次。单独删除 saveFileDialog1.ShowDialog(); 行,您已经在 if 条件中执行了该操作。

编辑:您还需要将所有 FileStream 操作移到 else block 内,以便在删除该行后它可以正常工作。这是我编辑的版本:

    private void SaveAsItem_Click(object sender, EventArgs e)
{
saveFileDialog1.FileName = "untitled";
saveFileDialog1.Filter = "Text (*.txt)|*.txt";
if (saveFileDialog1.ShowDialog()==DialogResult.Cancel)
{
richTextBox1.Text = "CANCEL";
issaved = false;
}
else
{
System.IO.StreamWriter SaveFile = new System.IO.StreamWriter(saveFileDialog1.FileName);
issaved = true;
SaveFile.WriteLine(richTextBox1.Text);
SaveFile.Close();
}
}

您也可以完全跳过 FileStream,只执行 File.WriteAlltext(saveFileDialog1.FileName, richTextBox1.Text)

关于c# - 关闭保存文件对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9576295/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com