我在关闭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块内,以使其在删除该行后正常工作。这是我的编辑版本:
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# - 关闭saveFileDialog,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9576295/