11 Comments

  • I did it by making my form a singleton. Quite a bit less code and I'm pretty sure we get the same results.

  • The singleton wouldn't work here because I would still expect an application to potentially have two or more instances of the MDI (while still only having a single instance of form X inside each MDI.



    Roy's approach seems to work quite OK.



    I'd be inclined to create a "Must Inherit" (sorry, I'm a VB programmer) form that implements the single form within an MDI functionality and then inherit all single-only forms from this one.



    Use the collection to keep track of instances within the base form as a "shared" variable and used a shared function to return an instance of a form if it can for the MDI for a given type. Then the MDI code is greatly simplified.



    Public Shared Function CreateMdiChildInstance(childType as Type, mdi as Form) as Form



    I hope that makes sense. Enjoy!

  • M.Eaton: A ginleton means it never gets destructed. I want my form do get destructed and not stay in memory throughout the whole application.

    Making it a singleton would make me do changes in the form code so that it does not get closed, am I wrong?

  • Luciano: I'm aware of the Singleton parttern - however I don't like implemnting it here for a few reasons:

    1. I will need to make code changes in all my forms *and* in the calling code. In my way the form has nothing to do with the loading implementation. Less code to debug and maintain.

    2. A singleton implies an object that is never Destroyed. Forms are "closed" = destroyed after a while. If I want my form to be a Singleton I would have to write code in my form that implements a "Hide on Close" logic because it will need to stay loaded all the time even if someone closed it. Otherwise the next time you call Show on the form after it has been closed you'll get a nasty "Cannot access a disposed object " error.



    I think my way is a little less messy than that plus it keeps all the logic in a separate place.

  • Roy, Ok...in the application I'm working on, I have a search screen. I want one, and only one, instance of this form. If it's minimized, I simply want it to popup the current instance. I can close it, but any way it goes, I will only ever be able to open one instance of it.



    I did this by creating a static GetInstance method. If there isn't an active instance, it will create one.



    Does that make sense?



    mike

  • M.Eaton: sure. Sounds like what I just implemented. But that's not a Singleton. It's a static factory. Where is the method sitting? inside the form? How is it keeping track of whether an instance is alive or not?

  • This was an interesting way to solve the problem. Thanks.

  • Hehe...yea, as I was typing the last post, I asked myself "am I using the correct terminology?"...live and learn. :-)

  • I should also say...the reason that I even said anything is because my method is quite a bit less code. Anyway...not that it matters. :-) I'm going to pull your code into a project and play with it.

  • Why not use the MdiChildren from the MDI parent instead of creating and keeping a separate list.

    private bool IsFormAlreadyLoaded(string formToLoadName)
    {
    foreach (Form frmChild in this.MdiChildren)
    {
    if (frmChild.Name == formToLoadName)
    {
    frmChild.Activate();
    return true;
    }
    }
    return false;
    }

  • agree with oliver :)

Comments have been disabled for this content.