Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How can MDI parent access controls on the active MDI child form?
 

How can MDI parent access controls on the active MDI child form?

I have a basic MDI application that does text editing in addition to some data processing. I'm trying to accesscontrols on the active MDI child form from the parent. For instance, when the "Save" menu option is clicked from the parent's menu bar, I'd like to save the text in the active MDI child form's ListBox.

I need C# examples for this, please. Thank you very much in advance!!!

annulik  Wednesday, August 13, 2008 6:07 PM
If all the MDI child forms are of the same type - then you can just expose the ListBox on the child form by either changing it's modifier or adding a property with a getter that will return the ListBox. Then, on the "Save" menu just cast the this.ActiveMdiChild to your form's type and use the property to get the ListBox.

If you have multiple types of MdiChild forms, you can either have all of them to inherit from one base form (with the ListBox and a property getter on it) or better off, just implement an interface with a ListBox property.

Code Snippet

// declare the interface
public interface IListBoxOwner
{
ListBox ListBox
{
get;
}
}

// form2 is one of the possible MdiChild forms
public partial class Form2 : Form, IListBoxOwner
{
public Form2()
{
InitializeComponent();
}

#region IListBoxOwner Members

public ListBox ListBox
{
get { return this.listBox1; }
}

#endregion
}

// in the MdiContainer form, the Save menu
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild is IListBoxOwner)
{
(this.ActiveMdiChild as IListBoxOwner).ListBox.Items.Add("asdasd");
}
}


hth,
Lior.
Lior Salem  Wednesday, August 13, 2008 10:03 PM
If all the MDI child forms are of the same type - then you can just expose the ListBox on the child form by either changing it's modifier or adding a property with a getter that will return the ListBox. Then, on the "Save" menu just cast the this.ActiveMdiChild to your form's type and use the property to get the ListBox.

If you have multiple types of MdiChild forms, you can either have all of them to inherit from one base form (with the ListBox and a property getter on it) or better off, just implement an interface with a ListBox property.

Code Snippet

// declare the interface
public interface IListBoxOwner
{
ListBox ListBox
{
get;
}
}

// form2 is one of the possible MdiChild forms
public partial class Form2 : Form, IListBoxOwner
{
public Form2()
{
InitializeComponent();
}

#region IListBoxOwner Members

public ListBox ListBox
{
get { return this.listBox1; }
}

#endregion
}

// in the MdiContainer form, the Save menu
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.ActiveMdiChild is IListBoxOwner)
{
(this.ActiveMdiChild as IListBoxOwner).ListBox.Items.Add("asdasd");
}
}


hth,
Lior.
Lior Salem  Wednesday, August 13, 2008 10:03 PM

You can use google to search for other answers

Custom Search

More Threads

• How to capture and save resources generated by CodeDomDesignerLoader in Custom Designer
• Global object
• Menu Bar
• Possible Bug in VS 2008
• Customize controls in VB.NET
• DataGridView - Edit columns after changing Dataset
• Wanted: Collapsable TreeView/ListView control
• Please help me!
• [solved] Display a designer form in an external application without freezing external application events
• Strategy for keeping properties in User Control synchd