Meditech NPR Reports / NPR Report Writing / NPR Report Writer / Meditech NPR / Non-Procedural Report / Meditech Consulting / Meditech Reports .NET 2.0: November 2007

Saturday, November 03, 2007

MDI Interface to Child Form (VS 2005 C#)

To control the child form from the parent form; you need to implement an interface to the child form. The interface will have publicly available methods which are accessible via the interface.

1) For your application's namespace, implement a public interface.

:public interface IChildForm;
:{
: // All methods to be accessed via the MDI parent must be here.
: void OpenFile();
: void SaveFile();
:}

2) Your child form implements the IChildForm interface by adding it to the list of implemented interfaces. The public methods listed in the IChildForm interface must be implemented in the form class.

:public partial class frmViewer :Form,IChildForm
:{
: public frmViewer()
: {
: InitializeComponent();
: }
:
: // You can call this from the IChildForm interface reference.
: public void SaveFile()
: {
: SaveFileDialog saveFileDialog = new SaveFileDialog();
: saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
: saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
: if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
: {
: string FileName = saveFileDialog.FileName;
: // TODO:Add code here to save the current contents of the form to a file.
: }
:
: }
:
: // You can call this from the IChildForm interface reference.
: public void OpenFile()
: {
: // TODO:Add code here to open the file.
:
; }
:
:}

3) Access the active MDI child.

:private IChildForm GetActiveChild()
:{
: // Get the active MDI child.
: Form ChildForm = this.ActiveMdiChild;
: // Access the save method via the interface.
: IChildForm iForm = ChildForm as IChildForm;
: return iForm;
:}

4) Call the Active MDI Child's interface methods.

:private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
:{
: IChildForm iForm = GetActiveChild();
: iForm.SaveFile();
:}
:
:private void OpenFile(object sender, EventArgs e)
:{
: IChildForm iForm = GetActiveChild();
: iForm.OpenFile();
:}

Meditech NPR Reports / NPR Report Writing / NPR Report Writer / Meditech NPR / Non-Procedural Report / Meditech Consulting / Meditech Reports