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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:35:01+00:00 2026-05-23T18:35:01+00:00

I have a host WinForm application extended with UserControls. Composition and instantiation work great

  • 0

I have a host WinForm application extended with UserControls. Composition and instantiation work great on initial startup. When I close the user control, I want to assume that it should be Disposed. However, this causes a problem when I want to show the user control again.

What I want to do is return the user control to it’s original state after I ensure the object has Disposed of any resources.

Here is one of the extension user controls:

[ExportMetadata ( "ModuleName", "ContactManager" )]
[Export ( typeof ( IUserModule ) )]
partial class ucxContactManager : IUserModule
{
    #region Fields
    //  Fields

    readonly string moduleName = "ContactManager";
    readonly string moduleDescription = "Contact Manager Extension Module";

    //  called from ucxContactManager constructor
    void InitializeUserModule ( )
    {
        Closing += ( o, e ) => Dispose ( );
        uxCloseContactForm.Click += ( o, e ) => Closing ( this, null );
    }

    //  IUserModule Members
    public event EventHandler<EventArgs> Closing = delegate { };
}

CompositionController does all the discovery and composition work, providing a method to request a specific module:

//  Lazily import all IUserModule interfaces
[ImportMany ( typeof ( IUserModule ), AllowRecomposition = true )]
public IEnumerable<Lazy<IUserModule, IQueryMetadata>> Modules { get; set; }

public bool TryGetModule ( string ModuleName, out IUserModule Module )
{
    var module = Modules.Where ( m => m.Metadata.ModuleName == ModuleName )
                        .Select ( m => m )
                        .FirstOrDefault ( );

    if (module != null)
    { Module = module.Value; }
    else
    { Module = null; }

    return Module != null;
}

Now, when we find the module, assigning module.Value causes the UserControl to be initialized – the constructor is called.

When the UserControl is closed and disposed, how can I return the module to this initial state?

  • 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-23T18:35:01+00:00Added an answer on May 23, 2026 at 6:35 pm

    I think your issue is that you are setting Module to module.Value, so you are instantiating the actual IUserModule through the Lazy<IUserModule, IQueryMetadata> instance. If you don’t need it until later on, why no set Module to the Lazy<IUserModule, IQueryMetadata> instance instead, and then create an instance of it when you need it.

    The other thing you may need to consider is lifetime of the part. Unless you are recomposing your Modules collection, you’ll only have one instance of the IUserModule because it is created and instantiated through the Lazy<,> instance. What you may want to consider is using an ExportFactory<IUserModule, IQueryMetadata> to spin up a new instance each time instead.

    ExportFactory is not part of .NET 4.0 (it made into Silverlight, but not the full .NET 4.0 BCL. Glenn Block released a version of it for .NET 4, which you can get here.

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

Sidebar

Related Questions

I have a WinForms user control Host with a custom UI Editor. Through that
I have an object created in a host application and can access it remotely
I have a problem with a WPF control that I'm trying to host in
I have the following control set as the Child of a WinForms element host
I have a winform application that controls some transmitters and sound cards. There is
I have been looking at Prism to host an old Winforms application. This is
Hi, We have a winform application that is only to be executed as a
I have a winForm panel that has a flash ative x control embedded in
I have a host and cPanel access but only one main user and password
I have a host application that controls various factory classes which produce implementations 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.