Display a form in a panel of another form

Posted on 2011-10-16
Last Modified: 2012-05-12
I have an mdi parent form with multiple child forms. From one child form (FormA) I select a treeview node. On another child form (SummaryForm) I have panels. I want to display a third form (formC) in the panel and pass it a value. I use the following code in FormA:

 Dim accountName As String = e.Node.Text
        With SummaryForm
        End With

Then is SummaryForm:

Public Sub FilterAccounts(ByVal accountName As String)
        Dim anAccountInfoForm As New AccountInfoForm()
        aCrmData = New CrmDataClass
        aCrmDataSet = aCrmData.getCrmDataSet
        AccountsBindingSource = New BindingSource
        With anAccountInfoForm
            .AccountFilter = accountName
            AccountsBindingSource.Filter = "Name = '" & accountName & "'"
            .NameTextBox.DataBindings.Add("text", AccountsBindingSource, "Name")
            .TopLevel = False
            .Dock = DockStyle.Fill
            .FormBorderStyle = Windows.Forms.FormBorderStyle.None
        End With

At runtime nothing happens, no exceptions, and the panel does not fill with the new form. What am I doing wrong?
Question by:fieldb1

    Author Comment

    Oh, in the above description I should have called formC anAccountInfoForm. Sorry.
    LVL 85

    Accepted Solution

    In FormA, you have:

            Dim accountName As String = e.Node.Text
            With SummaryForm
            End With

    Where did the reference to "SummaryForm" come from?  Do you have a local variable in FormA, called "SummaryForm"?

    I'm guessing that is the name of the Form right?

    ...and in the "main" form, you probably created an instance of SummaryForm to display it:

        Dim summary As New SummaryForm

    If this is the case, then you have a dynamic instance of SummaryForm being displayed on your main form, but in FormA you are referencing the "default instance".  These are not the same instances and is why AccountInfoForm never shows up.

    To fix this, you have several options:
    (a) Pass your actual instance of SummaryForm into FormA so that it can add the new form to the correct instance.
    (b) Use only the default instance of Summary Form (get rid of the "New" keyword and only use its NAME in the main Form).
    (c) Iterate over the OpenForms() collection to find SummaryForm so you can add the new form to the correct instance.

    Author Closing Comment

    Yes! Thank you! I knew it was something like that, but my brain just wouldn't work it out. Now, which of the three options above would you suggest? I implemented option 2 for now.
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Option (b) is good if you will only ever have ONE instance of that form displayed.  That is exactly the type of scenario that "default instances" were developed for.  It makes it easy to reference the same instance of the form, from anywhere, without having to pass the reference around or make it globally accessible.

    "Default instances" are a VB construct you wouldn't be able to port that directly to C# code.  For C#, you'd have to manually implement something similar to a singleton type pattern to get the same type of behavior.

    Pure OOP (Object Oriented Programming) frowns upon default instances as well.  If you don't care about C#, or pure OOP, then go ahead and use the default instances.  Just use them for the right scenarios and remember the difference between a "default instance" and a "dynamic instance".

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    This video discusses moving either the default database or any database to a new volume.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now