Solved

New instance of mdi child form being opened

Posted on 2008-06-24
9
375 Views
Last Modified: 2013-11-05
I have a mdi app the opens a child form when the app loads. I have a Public property on the child form that I set from the parent form.  When the app is started the property is initally set from the formload event of the child form.  Clicking the menu on the parent form sets the property to the desired value.
Issue:
When I first click the menu after opening the app a new instance of the form is created instead of changing the property on the existing child form.  After the new instance is created clicking the menu updates the property correctly and everything works fine.  It's only the first time the menu is clicked after opening the form that the new instance is created.  What am I doing wrong.
0
Comment
Question by:Moed
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 21857452
Sorry, i do not understand the following statement
"When I first click the menu after opening the app a new instance of the form is created instead of changing the property on the existing child form."

Which existing form do you mean ???
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21857546
What version VB.Net?

Also, we need to see two things:

(1) How you open the MDI child at app load.
(2) How you open the MDI child from the menu.
0
 
LVL 2

Author Comment

by:Moed
ID: 21859292
Version  vb 2005

This is from my parent form.load event

   Private Sub frmStart_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      StartupForm = Me
      'Debug.Print(My.Settings.DataLocation)
      sub_SetUpLog()
      sub_LoadGroupsItems()

      Dim frmCustomers = New frmCustomers
      frmCustomers.CustomerView = "ActiveCustomers"
      With frmCustomers
         .MdiParent = Me
         .Dock = DockStyle.Fill
         .Show()
      End With

This is from the explorer bar that I use for navigation

 Private Sub ExplorerBar_ItemClick(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinExplorerBar.ItemEventArgs) Handles ExplorerBar.ItemClick

      Select Case e.Item.Text
         Case "Current"
            If Not gvstrCurrentForm = "Customers" Then
               sub_CloseChildForms()
               Dim frmCustomers = New frmCustomers
               frmCustomers.CustomerView = "ActiveCustomers"
               With frmCustomers
                  .MdiParent = Me
                  .Dock = DockStyle.Fill
                  .Show()
               End With
               Me.statusLabel.Text = "Customers"
               gvstrCurrentForm = "Customers"
            Else
               frmCustomers.CustomerView = "ActiveCustomers"
            End If
         Case "Inactive"
            If Not gvstrCurrentForm = "Customers" Then
               sub_CloseChildForms()
               Dim frmCustomers = New frmCustomers
               frmCustomers.CustomerView = "Inactive"
               With frmCustomers
                  .MdiParent = Me
                  .Dock = DockStyle.Fill
                  .Show()
               End With
               Me.statusLabel.Text = "Customers"
               gvstrCurrentForm = "Customers"
            Else
               frmCustomers.CustomerView = "Inactive"
            End If
         Case "Completed"
            If Not gvstrCurrentForm = "Customers" Then
               sub_CloseChildForms()
               Dim frmCustomers = New frmCustomers
               frmCustomers.CustomerView = "Completed"
               With frmCustomers
                  .MdiParent = Me
                  .Dock = DockStyle.Fill
                  .Show()
               End With
               Me.statusLabel.Text = "Customers"
               gvstrCurrentForm = "Customers"
            Else
               frmCustomers.CustomerView = "Completed"
            End If
         Case "All"
            If Not gvstrCurrentForm = "Customers" Then
               sub_CloseChildForms()
               Dim frmCustomers = New frmCustomers
               frmCustomers.CustomerView = "AllCustomers"
               With frmCustomers
                  .MdiParent = Me
                  .Dock = DockStyle.Fill
                  .Show()
               End With
               Me.statusLabel.Text = "Customers"
               gvstrCurrentForm = "Customers"
            Else
               frmCustomers.CustomerView = "AllCustomers"
            End If
         Case "Active Warranty"
            If Not gvstrCurrentForm = "Customers" Then
               sub_CloseChildForms()
               Dim frmCustomers = New frmCustomers
               frmCustomers.CustomerView = "ActiveWarranty"
               With frmCustomers
                  .MdiParent = Me
                  .Dock = DockStyle.Fill
                  .Show()
               End With
               Me.statusLabel.Text = "Customers"
               gvstrCurrentForm = "Customers"
            Else
               frmCustomers.CustomerView = "ActiveWarranty"
            End If

The interesting thing is before I added the property on frmCustomers everything worked perfectly.  I needed additional views of the data so the proper code would be run on the form so I added the property and the problem showed up.
0
 
LVL 2

Author Comment

by:Moed
ID: 21859448
Sorry, i do not understand the following statement
"When I first click the menu after opening the app a new instance of the form is created instead of changing the property on the existing child form."

Which existing form do you mean ???
*************************
As you can see above the parent form initially opens the child form in the form load.  When I click the explorer bar and change views it loads a new instance of the child form but it only does this the first time I change views.  After that it operates as expected.
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 21859654
Here is your problem:

    Dim frmCustomers = New frmCustomers

You are using a LOCAL variable with the NEW keyword.

What this means is that each time this line is hit you are creating a NEW and DIFFERENT instance of the form and displaying it.

If you want to work with the EXISTING instance of the form then you must do one of two things:

(1) Keep a reference to the child form by declaring it at the class (Form) level so that it can be accessed from all interested subs/functions.
(2) Enumerate all open MDI child forms until you find your desired form.  This can be done using the MdiChildren() collection exposed by your MdiParent form.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21859696
A third option...

(3) Use the "default instance" of the form by using its NAME.  To do this you will need to COMMENT OUT the local declaration (or get rid of it):

    ' Note that I have COMMENTED OUT the local declaration below!
    ' Dim frmCustomers = New frmCustomers

Then you simply use the forms name to reference the default instance:

    frmCustomers.MdiParent = Me

Note that it looks the same as before...but since their is NO local variable called "frmCustomers" we are actually referencing the "default instance" of the form.  This feature was available in VB6, but went away in VB.Net 2001/2003.  It was brought back in VB.Net 2005 (and above)...
0
 
LVL 2

Author Comment

by:Moed
ID: 21860121
I used option three.   Worked perfect.  Thanks

So what is it about the default instance that makes it work?  I understand scope.... does it just have form level scope?
0
 
LVL 2

Author Closing Comment

by:Moed
ID: 31470199
Thanks so much.  Everytime I see you answer one of my questions I know its not just a shot in the dark, but an answer that will work and be clear if I am able to give enough information.    Thanks again
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 21860246
The "default instance" is basically a SHARED instance of the form.  You can access it from anywhere in your project.  When you set a form as the "Startup object" (thru Project --> Properties) it is the default instance of that form that is actually loaded.  =)

More info: http://msdn.microsoft.com/en-us/library/87y2hdsf(VS.80).aspx
Shared: http://msdn.microsoft.com/en-us/library/zc2b427x(VS.80).aspx
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

19 Experts available now in Live!

Get 1:1 Help Now