Solved

MDI Parent & Child in VB.NET

Posted on 2009-07-08
8
801 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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: …

706 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

18 Experts available now in Live!

Get 1:1 Help Now