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)
{
Stream SaveToFile = File.Open(saveToFileDialog.FileName,
FileMode.CreateNew);
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;
}
}
}