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

  • Home
  • SEARCH
  • 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 3356176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:29:00+00:00 2026-05-18T02:29:00+00:00

The first time I open one of my child forms from the main form

  • 0

The first time I open one of my child forms from the main form it opens normally. If I close the first instance of the child form and then reopen it however I get a crash the first time I try to call CreateGraphics() outside of the OnPaint() method. The exception I get is: “Cannot access a disposed object. Object name: ‘MyControlClass’.”

I’ve set breakpoints to monitor what’s going on. Dispose() is called as expected the first time I close the form. When I start the form the second time MyControlClass‘s constructor is called, and the Dispose method isn’t called prior to the exception. At the point of the exception this is still valid. Because of that I’m wondering if somehow it’s actually the static component of MyControlClass that ended up being disposed; not the instance object.

I am creating a new copy of the form each time the button to show it is called. MyChildForm is a member held by my mt parentform and is also used to prevent multiple copies of the form from being opened at once.

ShowMyForm()
{
    myChildForm = new myChildForm Form();
    myChildForm.FormClosed += myChildFormFormClosed;
    myChildForm.Show();
}

private void myChildFormFormClosed(object sender, FormClosedEventArgs e)
{
    myChildForm = null;
}

The line of code that crashes: MyControlClass inherits from MyControlClassBase, which in turn inherits from MyControlClassBaseBase. This line of code is triggered by a mouse event in MyControlClassBase and is in MyControlClassBaseBase. The code after this would take a cached image of MyControl, display it using the newly created Graphics object, and then draw an overlay based on the mouse cursor position.

Graphics g = CreateGraphics();

PS Since I’m sure someone will ask: The rube goldberg in question is due to the utter fail that is fake ‘transparency’ in winforms in any but the most trivial cases and the fact that MyControlClass takes too long to paint to keep up with the mouse cursor; but that’s a separate question entirely.

  • 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-05-18T02:29:01+00:00Added an answer on May 18, 2026 at 2:29 am

    After a form is closed, it is disposed – meaning that it exists just to read fields.

    If you want to show the same form again, create another instance or just hide it instead of closing.


    MyForm f = new MyForm();
    f.Show();
    // After closed, it will be disposed.
    

    So we need to do the same steps to show it again:

    f = new MyForm();
    f.Show();
    

    Now you will get a brand new and identical form.


    But to hide it when closed, you might need this code:

    private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
    
            Hide();
        }
    }
    

    Note that it will not work with modal forms. (ShowDialog();)
    (Thanks to Sorax) This will also not work with MDI children.

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

Sidebar

Related Questions

This is the first time I'm creating an open-source project, and I've decided (based
I'm trying to contribute to open source software for the first time, but I'm
I've got some strange bug: when I open page first time in some browser
My problem is everytime i open the website; first time the map doesn't show
I am creating a dialog on fly, when I open it the first time
i am first time using Popoverview, and i have one issue when i am
I want to avoid my child form from appearing many times when a user
I have first time installed th cron job for magento. THere is one module
I have created one distribution application also register custom URL scheme. First time after
I would like to open a settings window only once, first time i open

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.