Solved

MDI Parent & Child in VB.NET

Posted on 2009-07-08
8
804 Views
Last Modified: 2012-05-07
I have a MDI Application with a toolbar on the parent form.  This toolbar has the standard buttons, New, Save.  When I click the save button the values returned from controls on the child form are blank.  Example. txtName.text has a value but it is returning a null back to the routine in the parent form.

I have also tried using DirectCast(frmName,form) to run a routine from the child form but it still gives blank values for the controls.

What is the proper way to use the MDI Parent toolbar for child forms?  Specifically the Save control.  

Thank you,

David
0
Comment
Question by:dbote
  • 4
  • 3
8 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24810028
When the toolbar buttons are clicked in the MdiParent form, you can get the active MdiChild with ActiveMdiChild():
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activemdichild.aspx
0
 
LVL 1

Author Comment

by:dbote
ID: 24810214
I am not having trouble getting the active form.  The save routine is being called by the button on the MDI Parent and the controls with the data are on the MDI  Child.  The data being saved always returns a null value because the call starts from the MDI Parent.

If I move the save button to the MDI child everything is fine but that is not how a MDI application "should" work.

What is the best practice for interacting and saving data in an MDI Child form?  There is a lot of info out there about public variables but that just does not seem like the proper way to do it in .NET.
0
 
LVL 1

Author Comment

by:dbote
ID: 24813416
Having never done an MDI application in VB.NET I was uncertain how to communicate the data changes between the the parent and child forms.  Now that I have done some experimentation it is very easy.  Of course, I still do not know if this is the MDI standard way but this seems to work very well.

In my child form I create a Sub New for the form like below:

Public Sub New(byVal ClassName as Type)
   Me.InitializeComponent()
   retClass = ClassName    'retClass is a form level variable to HOLD the class info while the form is open
End Sub

In the parent form to call the child do something like this....

        Dim frmNewForm As New frmChildFormName(ClassNameToPass)
        frmNewForm.MdiParent = Me
        modShoulder = frmNewForm.show()

On the child form update the returning class periodically, whenever your application data changes.  The class on the parent form now reflects the changes.  No global variables necessary!

I don't think I left anything out and I may have made a typo but it works well for me.  I think the trick is realizing that forms are classes too and you can add more than the default functions and methods.

dbote




0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 250 total points
ID: 24814761
What you did was correct.If you want to initialize a form with some values while loading, you can use New constructor.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 24814928
There are MANY, MANY ways to go about it...and we can't really say what approach is "best" for your situation as we really don't have info about your application.

If you've found a solution that works then great!
0
 
LVL 1

Author Comment

by:dbote
ID: 24814959
So there really is not "best practice" for the exchange of data between MDI Parent and MDI Child forms?
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 250 total points
ID: 24815255
Nope...it really depends on what you're doing in your application.  You have to decide who is "driving"...the Parent or the Child.  The answer to this question often leads to a loosely coupled versus a tightly coupled solution.

Programming isn't an exact science...you have a bunch of techniques available to you and as the programmer you have to decide which approach or which COMBINATION of approaches best fits your needs.

Sometimes it doesn't matter which approach is used...sometime it does.  Choosing one approach may be easy now but then later turn out to be a horrible decision when the application needs to be extended or changed somehow.  ;)
0
 
LVL 1

Author Closing Comment

by:dbote
ID: 31601390
Thank you for the discussion points.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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