question

ChristKingKennedy-5906 avatar image
0 Votes"
ChristKingKennedy-5906 asked $$ANON_USER$$ answered

saving a disposed RichTextBox

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?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

$$ANON_USER$$ avatar image
0 Votes"
$$ANON_USER$$ answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

ChristKingKennedy-5906 avatar image
0 Votes"
ChristKingKennedy-5906 answered

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; }
         }
    
     }
 }
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.