Display a form in a panel of another form

Posted on 2011-10-16
Medium Priority
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
  • 2
  • 2

Author Comment

ID: 36976160
Oh, in the above description I should have called formC anAccountInfoForm. Sorry.
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 36976396
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

ID: 36976464
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 86

Expert Comment

by:Mike Tomlinson
ID: 36976486
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 though...so 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

807 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