MDI Interaction  
Author Message
Eragon.





PostPosted: Visual C# General, MDI Interaction Top

Hi,

I'm creating an MDI Form, and want to save what's in the child form(s) but directly from the parent, so I don't have to create multiple form menus. There are 10 different child forms, so would it be possible to allow it to save it with any form (but only if it is the active/top form)

For that matter, would it be possible to allow things such as undo/ redo/ cut/ copy/ paste/ find etc. likewise

Is it possible to use a save all function for pictures (i.e. creating moving GIF files)

Thanks.

Eragon




Visual C#16  
 
 
Language Burner





PostPosted: Visual C# General, MDI Interaction Top

luk eragon im not too sure, but i think if u use d IntelliSense it will show d objects if u do dis 4 example:

  1. Name ur mdi parent "mdiParent".
  2. Name ur 1st mdi child "firstMade"
  3. Name ur 2nd and following mdi children "secondMade", "thirdMade" etc.
  4. On firstMade, add an rtf box named "rtf".
  5. On mdiParent, add d folloing code into ur cut menu: mdiChild.firstMade.rtf.cut();
  6. Try it out

.



 
 
theblueeyz





PostPosted: Visual C# General, MDI Interaction Top

I have an application that does precisely the approach you're trying to use.

Here's what I did:

Create a base form that your MDI children will inherit from, and give it a Save() method, like so:

public class MyMDIBase : Form
{
public virtual void Save();

etc...
}


Then, have your MDI child forms inherit from it, and override the save method, and have the method do the actual work you'd want that form to do:

public class DataPage1 : MyMDIBase
{
public overrides void Save()
{
System.IO.File.WriteAllText("C:\", "blahblah");
}
}


NOW, in your MDI parent, when you want the child forms to save their data, simply iterate over the MDI forms, cast them to the MyMDIBase, which has a Save() method, and call Save():

public class MyMDIParent : Form
{
private void button1_Click(object sender, EventArgs e)
{
foreach(Form tForm in this.MdiChildren)
{
MyMDIBase mdibase = tForm as MyMDIBase;

if(mdibase != null)
{
mdibase.Save();
}
}
}
}

Why does this work Because all the compiler needs to know is that your forms are of type MyMDIBase, and that MyMDIBase has a method called Save().

At runtime, the CLR knows that each form is actually a derived form, e.g. DataPage1 or whatever, and it will call the actual method for that form.

You can even do really cool things with this kind of framework, like wire events into your main application, and set event handlers on the MDIChildren:

in your main form:

public void someButtonClick(object sender, EventArgs e)
{
DataPage1 newChild = new DataPage1();
this.Click += new EventHandler(newChild.SomeEventHandler());
}


And voila', your MDI children become aware of the parent, too.

Oh, I should add, the advantage to this approach is that the main application doesn't need to know ANYTHING about the children - it only needs to know what MyMDIBase can do.  This saves you from having to keep a bunch of member variables around to track all of your mdi children, keeping your code cleaner, easier to read, and MUCH more extensible than using hardcoded member variables.

I'm sure this approach has an official "Pattern" sort of name, but heck if I know what it is.  I came up with this on my own.