Access Parent MDI from with Child?

haze

Newcomer
Joined
Feb 1, 2003
Messages
2
I'm still learning the ropes in .NET and I'm having a tough time with this MDI problem.

I've got a parent MDI which loads a child MDI, after which I want to be able to access certain methods that are in the parent from withih the child. How can I do this?


Thanks!!
 
You can use MDIParent if you just need reference to a standard Form object. If your MDI form is exposing specific methods or properties, you'll need to cast the MDIParent as the particular class.

For example, if your MDI Form is called frmMain:
C#:
public class frmMain : Form
{
    public string MyCoolStringField;
    // The rest of frmMain
}

Then you can access it from another form with:
C#:
// Within Form1, say in a button click:
string s = ((frmMain)this.MDIParent).MyCoolStringField;

-nerseus
 
I'm having the same trouble here.

I'm using an MDI application that has around four or five childres. Each child window is oppened via a toolStrip bar. This bar has four buttons, and each button opens each child.

What I want to achieve is that if certain child is open (let's say customers), I don't want the user to open the rest of the childs until they close the customers child window.

I tried disabling each toolStrip button from the parent form, at the moment it loads it child, but then I don't have a way to reenable the disabled toolStrip button from the child form, only the parent form can do it.

I tried something like this, and it worked, but I don't like it.

Code:
FormChildIVA formChildy = FormHijoIVA.GetIVAinstance;
formChildy.Close();

GetIVAinstance is a property I made to get an instance of the FormChildIVA form.

I could do this for every child, but I prefer to disable the toolStrip buttons in the parent form. Easier for the user to understand and (I think) easier to program.

What are your suggestions?
 
I'm having the same trouble here.

What I want to achieve is that if certain child is open (let's say customers), I don't want the user to open the rest of the childs until they close the customers child window.

You may also use parent form's properties like,
Code:
Parent.ActiveMdiChild
or
Parent.MDIChildren.Count
to decide any mdichild is active or how many children are currently parented to this Form and decide whether to or not to display the other forms.
 
I found a way to do this, but I haven't completely implemented it. But my tests say that it works.

Taken from here:
http://www.vbforums.com/showthread.php?t=403165

The idea is to manipulate the events (load and close) of the child from from the parent form, instead the child form touching the parent form's members.

Visual Basic code:

Code:
Public Class Form1 
    Private WithEvents childForm As Form
 
    Private Sub OpenChildForm()
        If Me.childForm Is Nothing OrElse Me.childForm.IsDisposed Then
            'Create and display a new form.
            Me.childForm = New Form
            Me.childForm.MdiParent = Me
            Me.childForm.Show()
        Else
            'Focus the existing form.
            Me.childForm.Activate()
        End If
    End Sub
 
    Private Sub CloseChildForm()
        If Me.childForm IsNot Nothing AndAlso Not Me.childForm.IsDisposed Then
            Me.childForm.Close()
        End If
    End Sub
 
    Private Sub childForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles childForm.Load
        'The child form has just been opened.
    End Sub
 
    Private Sub childForm_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles childForm.FormClosed
        'The child form has just been closed.
    End Sub
 
End Class
C# code:
Code:
public class Form1
{
    private Form childForm;

    private void OpenChildForm()
    {
        if (this.childForm == null || this.childForm.IsDisposed)
        {
            //Create and display a new form.
            this.childForm = new Form();
            this.childForm.MdiParent = this;
            this.childForm.Show();
        }
        else
        {
            //Focus the existing form.
            this.childForm.Activate();
        }
    }

    private void CloseChildForm()
    {
        if (this.childForm != null && ! this.childForm.IsDisposed)
        {
            this.childForm.Close();
        }
    }

    private void childForm_Load(object sender, System.EventArgs e)
    {
        //The child form has just been opened.
    }

    private void childForm_FormClosed(object sender, System.Windows.Forms.FormClosedEventArgs e)
    {
        //The child form has just been closed.
    }


    public Form1()
    {

        //INSTANT C# NOTE: Converted event handler wireups:
        childForm.Load += new System.EventHandler(childForm_Load);
        childForm.FormClosed += new System.Windows.Forms.FormClosedEventHandler(childForm_FormClosed);
    }
}
I got the C# using a very cool tool called Instant C#, www.instantcsharp.com

So far, I managed to make a message box appear (calling it from the parent form) saying that certain child form has been oppened, when you open this child form.

Give it a try, guys.
 
I'm having some troubles here. I'm actually getting the result I want. I'm disabling the toolstrip buttons from a parent form via the child close and load events, but it only works the first time.

I'm sure it has something to do with the creating only one object, but I'm not sure where should I do this. I hiope you can help me.

This is what I have.

The code I use in the child form to generate only one of its kind.

C#:
        static private ChildFormInqui instance;        

        static public ChildFormInqui Instance
        {
            get
            {
                if (instance == null || instance.IsDisposed)
                {
                    instance = new ChildFormInqui();                    
                }
                return instance;
            }
        }

The next is the code I use to enable and disable the toolstrip buttons from the parent form, but only works the first time I close and open the ChildFormInqui form.

The Parent Form constructor, calling the delegates for the ChildInquiForm events, as expressed in the example I mentioned above.

C#:
public ParentForm()
        {
            InitializeComponent();

            if (ChildFormInqui.Instance != null || !ChildFormInqui.Instance.IsDisposed)
            {
                ChildFormInqui.Instance.Load += new System.EventHandler(ChildFormInqui_Load);
                ChildFormInqui.Instance.FormClosed += new System.Windows.Forms.FormClosedEventHandler(ChildFormInqui_FormClosed);
            }
        }

And finally, the definition of the FormClosing and Load events in the parent form.
C#:
private void ChildFormInqui_Load(object sender, System.EventArgs e)
        {
            ToolStripDocs.Enabled = false;
            ToolStripIVA.Enabled = false;
            ToolStripRentas.Enabled = false;
            ToolStripNotas.Enabled = false;
            ToolStripAgua.Enabled = false;
            ToolStripManto.Enabled = false;
        }

private void ChildForm_FormClosed(object sender, System.EventArgs e)
        {
            ToolStripDocs.Enabled = true;
            ToolStripIVA.Enabled = true;
            ToolStripRentas.Enabled = true;
            ToolStripNotas.Enabled = true;
            ToolStripAgua.Enabled = true;
            ToolStripManto.Enabled = true;
        }
 
The
C#:
ChildFormInqui.Instance.Load += new System.EventHandler(ChildFormInqui_Load);
                ChildFormInqui.Instance.FormClosed += new System.Windows.Forms.FormClosedEventHandler(ChildFormInqui_FormClosed);
bit of your posted code was what I meant.
 
The bit of your posted code was what I meant.

Sorry for being so much troubles, man. I added those two lines in the creation of the child form method, like this:

C#:
static private ChildFormInqui instancia;

static public ChildFormInqui Instancia
{
get
{
if (instancia == null || instancia.IsDisposed == true)
{
instancia = new FormHijoInqui();
ChildFormInqui.Instancia.Load += new System.EventHandler(ChildFormInqui_Load);
ChildFormInqui.Instancia.FormClosing += new System.Windows.Forms.FormClosingEventHandler(ChildFormInqui_FormClosing);
}
return instancia;
}
}
Then an error ocurred saying that both ChildFormInqui_Load and ChildFormInqui_FormClosing do not exist in that context, so I added the following ParentForm.ChildFormInqui_FormClosing (because those methds belong to the parent form), and it said you can't access them because its protection level, then I changed them to public in the parent form and now it says that I need an static atribute or something to access the object.

Now, I don't think I can make a static property returning an event. Probably I'm not "wiring up" correctly.

Sorry for all the problems, man, I'm just too dumb. What am I doing wrong? The problem is when I open the program and then open the ChildForm for the first time, it works; I close it, and it work; but if I reopen it, it stops working.
 
Last edited:
Oh, nevermind, I figured it out. I needed to use the wire up delegates at the toolstrip button click event.

C#:
private void BarraHerrInquilinos_Click(object sender, EventArgs e)
{
             FormHijoInqui formInqui = FormHijoInqui.Instancia;
             formInqui.Load += new System.EventHandler(FormHijoInqui_Load);
             formInqui.FormClosing += new System.Windows.Forms.FormClosingEventHandler(FormHijoInqui_FormClosing);                
                
             formInqui.MdiParent = this;
             formInqui.BringToFront();
             formInqui.Show();
}

I think this should be enough to control parent form's members through its children.
 
Back
Top