C#当按下savefiledialog取消按钮时如何使用mainform_formclosing返回表单
问题描述:
public void SaveToFile()
{
SaveFileDialog saveToFileDialog = new SaveFileDialog()
{
Filter = "Comma Separated Values (*.csv)|*.csv| Text File
(*.txt)|*.txt ",
FilterIndex = 0,
RestoreDirectory = true
};
if (saveToFileDialog.ShowDialog() == DialogResult.OK)
//After User Opens the DialogBox syste prepares to create a new file
{
Stream SaveToFile = File.Open(saveToFileDialog.FileName,
FileMode.CreateNew); //System cretae new file
StreamWriter FileToWrite = new StreamWriter(SaveToFile);
String[] contents =
ShowDataInScreenTxtb.Lines.ToArray();
for (int i = 0; i < contents.Length; i++)
{
FileToWrite.WriteLine(contents[i]);
}
DialogResult dialogResult = MessageBox.Show("Would you like to Refresh
the Screen ?", "Data has been Saved succesfully!",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dialogResult == DialogResult.Yes)
{
FileToWrite.Close();
ShowDataInScreenTxtb.Clear();
}
else if (dialogResult == DialogResult.No)
{
FileToWrite.Close();
}
}
if (saveToFileDialog.ShowDialog() == DialogResult.Cancel)
{
return;
}
}
Hi C# programmers little help here please. It work if save file menuItemlink but it does not work when I call savefile() method from MainForm_FormClosing
how to return to main form if savefiledialog cancel button pressed,
if I press the cancel button it continues asking until it close application. but it does not return to the application
我尝试了什么:
What I have tried:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ShowDataInScreenTxtb.Text == string.Empty)
{
DialogResult dialogResultExit = MessageBox.Show("Do you really want to
Close the Application?", "Close Alert!", MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dialogResultExit == DialogResult.Yes)
{
return;
}
else if (dialogResultExit == DialogResult.No)
{
e.Cancel = true;
}
}
if (ShowDataInScreenTxtb.Text != string.Empty)
{
DialogResult dialogForClose = MessageBox.Show("Data displayed on screen
will be deleted!" + "\n" + "\n" + "Would you like to Save it before
Closing the application?", "Save Data Alert",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
switch (dialogForClose)
{
case DialogResult.Yes:
this.SaveToFile();
break;
case DialogResult.No:
this.saveChanges = false;
break;
}
e.Cancel = this.saveChanges;
}
}
}
答
首先要注意的是你的缩进是错误的 - 如果您使用的是Visual Studio,则可能意味着您的代码无法编译。如果是这样,使用CTRL + K,D正确格式化文档,以便您可以看到发生了什么:
The first thing to note is that your indentation is wrong - which if you are using Visual Studio probably means that your code doesn't compile. If it does, use CTRL+K, D to format the document properly so you can see what is going on:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (ShowDataInScreenTxtb.Text == string.Empty)
{
DialogResult dialogResultExit = MessageBox.Show("Do you really want to Close the Application?",
"Close Alert!",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (dialogResultExit == DialogResult.Yes)
{
return;
}
else if (dialogResultExit == DialogResult.No)
{
e.Cancel = true;
}
}
if (ShowDataInScreenTxtb.Text != string.Empty)
{
DialogResult dialogForClose = MessageBox.Show("Data displayed on screen will be deleted!\n\n" +
"Would you like to Save it before Closing the application?",
"Save Data Alert",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
switch (dialogForClose)
{
case DialogResult.Yes:
this.SaveToFile();
break;
case DialogResult.No:
this.saveChanges = false;
break;
}
e.Cancel = this.saveChanges;
}
}
首先要注意的是 this.saveChanges
只能在一个分支中设置 - 当dialogForClose为No时,将其设置为false - 当值为Yes时,它的值为unknown / indeterminate。当结果为是时将其设置为true可以解决您的问题。
The first thing to note is that this.saveChanges
is only set in one branch - when dialogForClose is "No" you set it to false - so it's value is unknown / indeterminate when the value is "Yes". Setting it to true when the result is "Yes" may solve your problem.