Display a form in a panel of another form

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
            .FilterAccounts(accountName)
        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
            Me.DetailPanel.Controls.Add(anAccountInfoForm)
            .Show()
        End With

At runtime nothing happens, no exceptions, and the panel does not fill with the new form. What am I doing wrong?
fieldb1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

fieldb1Author Commented:
Oh, in the above description I should have called formC anAccountInfoForm. Sorry.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
In FormA, you have:

        Dim accountName As String = e.Node.Text
        With SummaryForm
            .FilterAccounts(accountName)
        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
    ...
    summary.Show()

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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fieldb1Author Commented:
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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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".
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.