Click here to Skip to main content
15,897,187 members

C# how to return to form using mainform_formclosing when savefiledialog cancel button is pressed

JimB_ asked:

Open original thread
C#
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:

C#
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;
   }         
  }
  }
Tags: C#, Dialog, MessageBox

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900