Passing Parent information to ActiveMdiChild

DiverDan

Contributor
Joined
Jan 16, 2003
Messages
645
Location
Sacramento, CA
I have a parent form that performs various calculations. I would like to know how to pass the results to the ActiveMdiChild's listview. Since there might be several MDIChild forms created, it is important that the results are only passed to the listview of the ActiveMdiChild.
 
You have a couple of options...
First, do you really want the parent to pass data to the child, or have the active child "pull" something from the parent?

If you really want the parent form to pass data to the active child window, you'll need to somehow get a specific interface to the child. If you know that all of the child windows are of type Class2 for instance, you can cast the ActiveMdiChild property as Class2 and then call any public methods on that form:
C#:
// From the parent form...
((Class2)this.ActiveMdiChild).SetSpecialData(this.myDataSet);

You can also define an interface that all child forms inherit. As above, you can cast the child form as the interface to make the function call. For example:
C#:
// Define the interface:
public interface ISpecialForm
{
	public void SetSpecialData(object data);
}

// In Class2, a child form:
public class Class2 : System.Windows.Forms.Form, ISpecialForm
{
	public void SetSpecialData(object data)
	{
		// Receive the data...
	}
}

// From within the MDIParent form:
((ISpecialForm)this.ActiveMdiChild).SetSpecialData(this.myDataSet);

-ner
 
Okay...can you translate this into VB.net lingo so that I do not misunderstand anything you have written. And pulling from the active child is new to me. Would this be a better option? If all the child forms are created from the same form then would all child forms receive the calculated data? All the action takes place when a calculate button is pressed. With this specific event can pulling from the parent be more effictive then passing to the active child?
 
Hi

Translates to:

Visual Basic:
' From the parent form...
 CType(me.ActiveMdiChild, Class2).SetSpecialData(me.myDataSet)

and..

Visual Basic:
' Define the interface:
Public Interface ISpecialForm

    Function SetSpecialData(data as Object);

End Interface

' In Class2, a child form:
Public Class Class2
  Inherits System.Windows.Forms.Form
  Implements ISpecialForm

    Public Function SetSpecial() As Object Implements Class1.SetSpecial
 
' Receive the data...

    End Function        


' From within the MDIParent form:
CType(me.ActiveMdiChild,ISpecialForm).SetSpecialData(me.myDataSet)

I suggest you use Nerseus's example above and create all child mdichild forms, implementing this type of interface.
This way you will know that all child forms will have the 'SetSpecialData' function. (Note you can obviosly change it to a property)

You can also use events and delegates in this senario. You can, on creation of these mdichildren, let them subscribe to a parent event, that gets fired when you want to pass the data. This will however fire onto all subscribed children, not just the activechild.

Hope this helps
 
Thanks for the VB conversion :)

One note:
This way you will know that all child forms will have the 'SetSpecialData' function. (Note you can obviosly change it to a property)

I don't know about VB.NET, but interfaces can't implement properties in C#.

-nerseus
 
Thanks for the help....but I am a bit too new to VB to totally understand and implement this. Could you make it a bit more plan for me? If I add the define code to all childs and the CType Dataset code in the parent...then how can I pass the information to the ActiveMdiChild listview?
 
Well this piece of code:

Visual Basic:
' From within the MDIParent form:
CType(me.ActiveMdiChild,ISpecialForm).SetSpecialData(me.myDataSet)

will pass the information to the active child.

Note that the example given to you will pass a dataset to the child, but you can use any type of data format (array's, hastable, etc... whichever makes more sense in your design)
 
Back
Top