Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8172423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:46:56+00:00 2026-06-06T21:46:56+00:00

I have a child form opened from my main form as follows: var cf

  • 0

I have a child form opened from my main form as follows:

var cf = new ChildForm { Owner = this };
cf.Show();

The child form then does some drawing on another thread.

When a user tries to close the main form – if the child form is open – then a FormClosing event is fired first in ChildForm and then the same event is raised in the main form. On a FormClosing event the child form stops its drawing thread.

Users may try to close the main form when it contains unsaved data. Then they are shown a warning “Data not saved. Cancel close?“, by the main form’s FormClosing event handler. They can then cancel the save (i.e. the Cancel flag is set on the FormClosingEventArgs object by the main form’s FormClosing event handler).

However, by that point, the child form’s FormClosing event has already been raised, and it will have stopped its drawing thread. The child form does not know that it should now continue drawing (as if nothing had happened).

Is it possible to detect from the child form that the FormClosing event was cancelled by the main form? I would still like to stop the redrawing thread while the user is being asked to save data in the main form.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T21:46:57+00:00Added an answer on June 6, 2026 at 9:46 pm

    I would provide a solution based on interfaces. This way will be easy for you to have an uniform way of managing if the application can be closed or not. With the following implementation the parent-form is in charge of asking the child-window if is ready to be closed, the child does whatever actions has to be done and replies to main window.

    Let suppose I have the interface IManagedForm:

    interface IManagedForm
    {
        bool CanIBeClosed(Object someParams);
    }
    

    Both forms (Form1 and ChildForm) would implement it.

    Note that for this example I’m instantiating the ChildForm in this way:

    ChildForm cf = new ChildForm() { Owner = this, Name = "ChildForm" };
    cf.Show();
    

    Here comes first the implementation of the interface by Form1:

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        object someArgsInterestingForTheMethod = new object();
    
        e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
    }
    
    // Ask the ChildForm it is done. If not the user should not leave the application.
    public bool CanIBeClosed(object someParams)
    {
        bool isOKforClosing = true;
    
        var cf = this.Controls["ChildForm"] as IManagedForm;
    
        if (cf != null)
        {
            isOKforClosing = cf.CanIBeClosed(someParams);
            if (!isOKforClosing)
            {
                MessageBox.Show("ChildForm does not allow me to close.", "Form1", MessageBoxButtons.OK);
            }
        }
        return isOKforClosing;
    }
    

    And finally your ChildForm implementation of the interface would look like this:

    private void ChildForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        object someArgsInterestingForTheMethod = new object();
    
        e.Cancel = !((IManagedForm)this).CanIBeClosed(someArgsInterestingForTheMethod);
    }
    
    public bool CanIBeClosed(object someParams)
    {
        // This flag would control if this window has not pending changes.
        bool meetConditions = ValidateClosingConditions(someParams);
        // If there were pending changes, but the user decided to not discard
        // them an proceed saving, this flag says to the parent that this form
        // is done, therefore is ready to be closed.
        bool iAmReadyToBeClosed = true;
    
        // There are unsaved changed. Ask the user what to do.
        if (!meetConditions)
        {
            // YES => OK Save pending changes and exit.
            // NO => Do not save pending changes and exit.
            // CANCEL => Cancel closing, just do nothing.
            switch (MessageBox.Show("Save changes before exit?", "MyChildForm", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question))
            {
                case DialogResult.Yes:
                    // Store data and leave...
                    iAmReadyToBeClosed = true;
                    break;
                case DialogResult.No:
                    // Do not store data, just leave...
                    iAmReadyToBeClosed = true;
                    break;
                case DialogResult.Cancel:
                    // Do not leave...
                    iAmReadyToBeClosed = false;
                    break;
            }
        }
        return iAmReadyToBeClosed;
    }
    
    // This is just a dummy method just for testing
    public bool ValidateClosingConditions(object someParams)
    {
        Random rnd = new Random();
        return ((rnd.Next(10) % 2) == 0);
    }
    

    Hope it is clear enough.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a main form, and a floating child form that is non-modal. The
I have a nested form Parent, which accepts attribute for Child. In my controller#new,
I have a project with a main MDI form. There is a child MDI
I have page control in main form and page controls in child form ,
I hope I can explain this clearly enough. I have my main form (A)
I have a child form that I load from a parent form. The parent
I have a non-modal child form which opens up from a parent form. I
I have a need to close a parent form from within child form from
I have a main windows form and another form that inherits from it. The
I have one Main form and two types of child form MainForm ChildFormA -

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.