Solved

MDI Parent & Child in VB.NET

Posted on 2009-07-08
8
814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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 86

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 86

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

732 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