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 661415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:15:52+00:00 2026-05-13T23:15:52+00:00

Long-time joelonsoftware follower, 1st-time stackoverflow poster. I want to know how safely I can

  • 0

Long-time joelonsoftware follower, 1st-time stackoverflow poster.

I want to know “how safely” I can do the following (C#):

Form formDlg = new Form();
TextBox box = new TextBox();
formDlg.Controls.Add( box );
formDlg.ShowDialog();
formDlg.Dispose();
string sUserEntered = box.Text; // After parent Dispose'd!

In practice, this (apparently) works, because box (as a Control) has a private text field (a string) which it uses to implement its Text property after its window handle is destroyed.

I won’t be satisfied by a general answer that “you can’t access an object after it’s Disposed” because (1) I can’t find any such blanket prohibition in MS docs, (2) I’m not accessing an unmanaged resource, and (3) this code doesn’t throw any exception (including ObjectDisposedException).

I would like to do this so I can create and use a combined “ShowAndDispose” method to reduce the risk of forgetting to always call Dispose() after ShowDialog().

To complicate, the behavior changes in the debugger. If I break before Dispose(); then Quick Watch box and drill down into its Control base class; then step past Dispose(); then box.Text returns “”! In other scenarios box.Text returns the user-entered text.

  • 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-13T23:15:52+00:00Added an answer on May 13, 2026 at 11:15 pm

    It is an implementation detail that this code runs without a problem. The Control.Text property happens to be cached by the Control class so disposing the TextBox doesn’t cause an ObjectDisposed exception.

    That’s fairly rare btw, lots of control property getters and setters generate a Windows message to ask the native Window control for the property value. You’ll get a kaboom on those because the Handle property is no longer valid. Notable also is that the Text property setter updates the cached value but also generates a Window message to update the native control. Kaboom here.

    I assume this is just general interest, don’t ever use code like that in your program. Well, you’d find out quick enough.

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

Sidebar

Related Questions

Long time lurker, first time poster. I have a page that can dynamically (with
Long time stackoverflow reader, first time poster. Forgive me if I'm not asking a
(Long time reader of SO, first time asking a q. I'm quite new to
Long time reader, first time poster. Any help is greatly appreciated. I have crafted
A long time ago I had the following directory structure in my SVN repository
Long time browser, first time asker... Maybe you can help. I've searched and searched,
long time reader, first time poster. I’m coming with an issue that many of
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
Not long time before I've discovered, that new dynamic keyword doesn't work well with
Long time Windows developer, 1st time Objective-C/iPhone developer wants to create a NSDictionary of

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.