Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

MDI Parent & Child in VB.NET

Posted on 2009-07-08
8
Medium Priority
?
825 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Accepted Solution

by:
srikanthreddyn143 earned 750 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 750 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

636 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