Question MDI Forms

tushar_g149

Member
Joined
Jan 22, 2011
Messages
7
Programming Experience
3-5
Hi,

I have question related to MDI form.

I have MDI form in different project and child forms in different project which is dll project.

How can I call save method of my child form from MDI form when i click on save from toolbar button of mdi parent form.

I can't access child forms save method at run time???

any response would be appreciated...

thank you very much in advance.

Regards,
Tushar
 
I can't access child forms save method at run time???
Of course you can. If you can access the form then you can access the form's members. Presumably you are accessing the child form via the parent's ActiveMdiChild property, which is type Form. As such, you must cast it as the form's actual type, for which you would use DirectCast or TryCast.
 
Have tried this but couldn't find save method of child form. Since my child form is coming from component (i.e. in seperate dll). If I will give the reference of this dll then it shows me save method at design time if I will create object of this form. But I would like to make my save functionality in MDI form more generic that is it should work for each and every child form (Ofcouse for that every child form should have same method)

Can I access this save method without giving reference of my component.
 
You have two options, a bad one and a good one:

1. Bad: Turn Option Strict Off for the project or for the parent form code file. You can then call the Save method without the compiler having to know that it exists.

2. Good: Declare an interface with a Save method and have all your child forms implement that interface. You can then cast every child form as that interface type and call its Save method.
 
2b. Also good: Use a base class (form) that includes the Save method, override it in inherited forms. You can then use the base form type for interaction.
3. perhaps less good: Attach parent button Click to event handler in child form dynamically using MdiChildActivate Event and the childs Actived event.
 
Thank you very much for all your replies guys.

Ok. Lets say I will go with that interface type but for that do I need to Import my DLL in which my child form resides (i.e. from MDI project from where I am calling child forms)? Where should I keep my interface? Is it in dll component itself or in MDI form project?

If I will create interface in component project than how would I implement it without giving reference of this component in my MDI parent project?

Ok so you mean to say I will create interface in my MDI parent project (lets called my is basically called Framework...and in my component project i should refer this framework component and then have all my child forms implement this interface from framework project? Is that right?

After implementing above will I be able to get save method at run time without turning Option strict off? I am defining my child form names in menuitem.tag property. Will I be able to use that save method at run time as well as design time?

I am sorry but I couldn't understood how can I implement form bit which JohH has explained in this reply.

Would appreicate if anyone of you can explain this with example please?
 
If I will create interface in component project than how would I implement it without giving reference of this component in my MDI parent project?
What do you mean by that? Aren't you already referencing the dll?
If the dll is to use a type it must either define it or reference the assembly that does.
 
Hi John

Apologies for late reply.

Yes i am not adding reference of my component project to my framework project. Actually i have found that withtout adding reference also you can call form from another project (component project in this case) in following way using reflection.
Dim passem As Assembly = Assembly.LoadFrom("AssemblyName")
abc = DirectCast(passem.CreateInstance("Classname"), Form)

with this i can access form and it's method but with the cost of OPTION STRICT OFF which i dont want.

Now what I have done is i have created interface with save method in my Framework project and accessing that interface in my component project i.e. i am implemeting this interface in each one of my child forms but when I try to call save method from parent form project(i.e.Framework) using following statement it gives me error saying unable to cast from form to interface...following is the statement i am using for direct casting..

DirectCast(Me.ActiveMdiChild, IWindow).save()

Any help is as always appreciated....

Regards,
Tushar
 
My first guess would be that you have two different interfaces. You have to declare the interface in one place only and use that one interface in both places. If you parent form and child form are in different projects then the interface must be declared in a third project. The other two projects MUST reference that third project/assembly.
 
Hi,

Currently I have two projects Framework (which contains MDI form i.e. main form) and Component (child forms). So you mean to say that i need to put the interface in another project and both of these project need to reference it. Will it still work? I mean as you know from my last post that I am calling child form from MDI form using Reflection i.e. using following statement.

Dim passem As Assembly = Assembly.LoadFrom("AssemblyName along with the path")
abc = DirectCast(passem.CreateInstance("Classname"), Form)

I have checked at run time it does gives me the child form object but some how when i direct cast it, it gives me error?
 
Hi jmcilhinney

I just tried your solution and it worked.
Thank you very much for your help.

And also thanks to JohnH for his help.

Thanks once again every one.

Cheers,
Tushar
 
Back
Top