Notepad SaveFile Dialog C# buttons configuring

Hemanth B 886 Reputation points
2021-06-10T17:01:43.083+00:00

Hi, I created a notepad in c#, and added this code:
What this code does is that when the user writes something in the notepad and closes without saving previously, a message box appears saying "You have unsaved changes, do you want to save them" (yes or no buttons in the message box) now when Yes button of message box is clicked, a save dialog box appears (the file explorer dialog box). Next when the user clicks click the close button (X Button) on top of the file explorer dialog (As shown in the image below), the entire application closes instead of only the file explorer dialog box closing and the user's work is lost! Please help me with this problem.

104278-102634-sc.png

saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();  
            saveFileDialog1.Filter = "Text file|*.txt|All files|*.*";  
            saveFileDialog1.Title = "Save as";  
            saveFileDialog1.FileName = "MyTextDocument1";  
            if (saveFileDialog1.ShowDialog() != DialogResult.OK)  
            {  
                Debug.WriteLine("This is hemanth debug2");  
                return false;  
            }  
                 
            else if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)  
            {  
                Debug.WriteLine("This is hemanth debug3");  
                return true;  
  
            }  
            if (saveFileDialog1.FileName == "")  
                return false;  
            try  
            {  
                using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName))  
                {  
                    sw.Write(richTextBox1.Text);  
                    Filename = saveFileDialog1.FileName;  
                }  
            }  
            catch (Exception ex)  
            {  
                MessageBox.Show("Write error: " + ex.Message);  
                return false;  
            }  
            return true;  
        }  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,844 questions
{count} votes

Accepted answer
  1. Timon Yang-MSFT 9,591 Reputation points
    2021-06-11T05:36:10.01+00:00

    I got the problem. In fact, this is the correct phenomenon.

    When we click to exit, the code enters the OnFormClosing method, where AskToSave() is called.

            protected override void OnFormClosing(FormClosingEventArgs e)  
            {  
                AskToSave();  
            }  
    

    We did not perform any check on its return value. No matter the user chooses to exit or save in the file explorer dialog, this method can be executed normally, and there is no reason to prevent the program from closing normally.

    If you want the user to think about whether to exit, you can pop up a MessageBox:

            protected override void OnFormClosing(FormClosingEventArgs e)  
            {  
                AskToSave();  
                var window = MessageBox.Show(  
                              "Close the window?",  
                              "Are you sure?",  
                              MessageBoxButtons.YesNo);  
                e.Cancel = (window == DialogResult.No);  
            }  
    

    Update(6/23):

    If you don't want to use an additional MessageBox, we need to add a field and modify several codes.

            private bool isCancel = false;  
            protected override void OnFormClosing(FormClosingEventArgs e)  
            {  
                AskToSave();  
                e.Cancel = isCancel;  
            }  
    

    In the SaveAs() method:

                if (saveFileDialog1.ShowDialog() != DialogResult.OK)  
                {  
                    isCancel = true;  
                    Debug.WriteLine("This is hemanth debug2");  
                    return false;  
                }  
      
                else if (saveFileDialog1.ShowDialog() == DialogResult.Cancel)  
                {  
                    Debug.WriteLine("This is hemanth debug3");  
                    return true;  
                }  
    

    I have a question here, saveFileDialog1.ShowDialog() != DialogResult.OK includes saveFileDialog1.ShowDialog() == DialogResult.Cancel, so the code in else if will never be executed. Is this an unexpected error?

    In the AskToSave() method:

                if (dr != DialogResult.Yes)  
                {  
                    isCancel = false;  
                    return;  
                }  
    

    If there are still any problems with the code, please feel free to let me know.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.