Click here to Skip to main content
15,860,972 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I am building an mdi application.

In the mdi parent form I have a toolstrip which conatins new and open button.
In the childforms I have toolstrip which has save and submit buttons.

If I open the childform then the toolstrip of the childform gets merged with the toolstrip of the mdi parent so that the toolstrip contains new, open, save and submit buttons.

My question is: How can I implement, if the new button is clicked, if the mdi child form is opened then the corresponding function in the child form gets executed if it exists otherwise the function in the mdi parent form gets executed.
Posted
Updated 17-Apr-11 21:27pm
v2
Comments
Dalek Dave 18-Apr-11 3:28am    
Edited for Grammar, Spelling and Readability.

Make sure you've created an event handler for the MdiChildActivate event of your MDI Parent form and use the following code :
C#
private void Main_MdiChildActivate(object sender, EventArgs e)
{
ToolStripManager.RevertMerge(toolMain);

MdiChildForm frmChild = ActiveMdiChild as MdiChildForm;
if (frmChild != null)
{
    // The frmChild.FormToolStrip is a property that exposes the
    // toolstrip on your child form
    ToolStripManager.Merge(frmChild.FormToolStrip, toolMain);
}


That's all, you're good to go!
Good luck,
Eduard
 
Share this answer
 
Comments
Dalek Dave 18-Apr-11 3:28am    
Good Answer.
Kunal Singha Roy 18-Apr-11 10:03am    
But I have done this merging and demerging of toolstrip.My question is how to execute a function within childform when the button in the toolstrip of the parent mdi form is clicked.
Eduard Keilholz 18-Apr-11 11:02am    
Add the toolstrip button on the childform itself, handle the click event and execute the method you want to execute. Merge the toolstrip of the childform into the toolstrip on you MDI Parent using the code above.
Kunal Singha Roy 19-Apr-11 1:39am    
I could have done that but the requirment of my application is different.The mdi parent form toolstrip conatins the button named new.For diffrent childforms we have to execute different code when the new button in mdi parent forms toolstrip is clicked.
Eduard Keilholz 19-Apr-11 2:35am    
That is exactly what it'll do if you did what I told you!!!
OK, once again... I hop you'll get it now :

Add a ToolStrip to your MDI Parent window. Add a button to the toolstrip and name it (for example) toolButtonNew. Set the Text property of toolButtonNew to New.

Now create a new form (which will be your MDI Child form and add a ToolStrip to the child form. Add a button to the toolstrip and name it (for example) mdiChildToolNew. Set the Text property of the mdiChildToolNew to New (this must be the same as the Text property value of the tool button on the MDI Parent). Now this is important set the MergeAction property to Replace.

Now go back to your MDI Parent form, and add an event handler for the MdiChildActivate event and add the follwoing code :
C#
private void Main_MdiChildActivate(object sender, EventArgs e)
{
    ToolStripManager.RevertMerge(toolMain);

    MdiChildForm frmChild = ActiveMdiChild as MdiChildForm;
    if (frmChild != null)
    {
        // The frmChild.FormToolStrip is a property that exposes the
        // toolstrip on your child form
        ToolStripManager.Merge(frmChild.FormToolStrip, toolMain);
    }
}


The result is that your MDI Parent always contains a new button, which will fire on the MDI Parent. Now if you open the MDI Child form (as MDI Child of the MDI Parent), the code above will replace the button on the ToolStrip of your MDI Parent and then the event will fire on the MDI Child. If you wish to add a button from the MDI Child to the MDI Parent toolstrip, set the MergeAction property to the desired value (for example Insert).

I hop you'll get it now!

Eduard
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900