Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
   }         
  }
  }
Posted
Updated 18-Sep-18 19:59pm

1 solution

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;
        }
    }
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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