saving a disposed RichTextBox

Christ Kennedy 196 Reputation points
2021-06-05T10:50:45.547+00:00

i'm working on a WordProcessor.
I want the form to ask the user whether or not he wants to save his work after the user has pressed the 'x'(dispose) button and killed the form.
I have an event_handler property in my class that displays the RichTextBox and that event is called whenever the RichTextBox is disposed.

but

the messagebox asking the user "do you want to save your work" & the "SaveFileDialog" (I tried both these separately) are not treated like the .ShowDialog() forms that they are and do not wait for a user response.

is there a way to catch the program flow when the user clicks the 'x'(dispose) button BUT before the app actually starts disposing everything?

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,320 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-05T12:10:21.357+00:00

    Hello, try putting your save code inside of the Form Closing event. That event will fire when the user clicks the 'x' button, but before the richtextbox disposes. For example:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (MessageBox.Show("Do you want to save your work?", "save your work", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    if (sfd.ShowDialog() == DialogResult.OK)
                        cRTX.rtx.SaveFile(sfd.FileName);
                }
            }
    

    I hope it helps.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Christ Kennedy 196 Reputation points
    2021-06-05T11:00:07.127+00:00

    here's a sample of my code

    namespace Save_Disposed_Form_MSDN_example
    {
        public partial class Form1 : Form
        {
            classRichTextBox cRTX = new classRichTextBox();
            public Form1()
            {
                InitializeComponent();
                Controls.Add(cRTX);
                cRTX.Dock = DockStyle.Fill;
                cRTX.eventHandler_Dispose = saveWork;
    
                Text = "click the 'x' dispose button to demonstrate problem.";
            }
    
            void saveWork(object sender, EventArgs e)
            {
                if (MessageBox.Show("Do you want to save your work?", "save your work", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    if (sfd.ShowDialog() == DialogResult.OK)
                        cRTX.rtx.SaveFile(sfd.FileName);
                }
            }
        }
    
    
        public class classRichTextBox : Panel
        {
    
            public RichTextBox rtx = new RichTextBox();
            public classRichTextBox()
            {
                Controls.Add(rtx);
                rtx.Dock = DockStyle.Fill;
                rtx.Disposed += Rtx_Disposed;
            }
    
            private void Rtx_Disposed(object sender, EventArgs e)
            {
                if (eventHandler_Dispose != null)
                    eventHandler_Dispose((object)this, new EventArgs());
            }
    
            EventHandler _eventHandler_Dispose = null;
            public EventHandler eventHandler_Dispose
            {
                get { return _eventHandler_Dispose; }
                set { _eventHandler_Dispose = value; }
            }
    
        }
    }
    
    0 comments No comments