我关闭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();
}
答案 0 :(得分:6)
您正在调用saveFileDialog1.ShowDialog()
两次,一次显示它,一次调用结果。你只需要调用一次。删除单词saveFileDialog1.ShowDialog();
的行,您已经在if
条件内执行此操作。
编辑:您还需要移动else块中的所有FileStream
操作,以便在删除该行后正常工作。这是我编辑的版本:
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)
。