Solved

New instance of mdi child form being opened

Posted on 2008-06-24
9
379 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

789 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