Solved

MDI Parent & Child in VB.NET

Posted on 2009-07-08
8
802 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
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.

948 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

22 Experts available now in Live!

Get 1:1 Help Now