Hi all
I want to call a Mdi Child window User defined function from the MDI toolbar button click in c#. How can I achieve this?
Printable View
Hi all
I want to call a Mdi Child window User defined function from the MDI toolbar button click in c#. How can I achieve this?
It is just like calling any method of any class. You need a reference to the form and the function must be declared public. In the ButtonClick event handler for the Toolbar you query the e.Button property to determine which button was clicked and act accordingly.
:confused: :confused: :confused:Quote:
Originally Posted by jmcilhinney
I got the refernece to the form from the this.ActiveMdiChild property, since the Solution contains a lot of MDI Childs it is important that I use this property and determine the current active form. However, if I declare a method as public on a few forms then how do I call that method using this property only and not by creating an instance of the form explicitly.
The ActiveMdiChild property is of type Form, so you need to cast it as the correct type so you can call methods of that type. Let's say you have two form classes, named ChildForm1 and ChildForm2, that you create instances of. You might have a method named DoSomething in each class that may or may not do the same thing. If you want to call the appropriate method when the DoSomething button is clicked on the toolbar you would do something like this:Code:private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if (e.Button == this.doSomethingButton)
{
Type activeChildType = this.ActiveMdiChild.GetType();
if (activeChildType == typeof(ChildForm1))
{
((ChildForm1)this.ActiveMdiChild).DoSomething();
}
else if (activeChildType == typeof(ChildForm2))
{
((ChildForm2)this.ActiveMdiChild).DoSomething();
}
}
}
If there are about a 100 child forms in the project then do I repeatQuote:
Originally Posted by jmcilhinney
VB Code:
if (activeChildType == typeof(ChildForm1)) { ((ChildForm1)this.ActiveMdiChild).DoSomething(); } else if (activeChildType == typeof(ChildForm2)) { ((ChildForm2)this.ActiveMdiChild).DoSomething(); }
for 100 times / constructs?
It sounds like you should be having the child forms provide their own menus and toolbars and then merge them with the parent's. That way there's no need to cast the ActiveMdiChild as the menu items or toolbar buttons being clicked will actually be part of the child form.
Actually I dont want any menus / tool bar on the Child Forms, because this involves creating the same toolbar 100 times for 100 different forms. What I actually wanted is the toolbar to be placed on the MDI and the toolbar buttons calling various methods from the child forms. These methods will be having the same name and will be existant in all the forms.Quote:
Originally Posted by jmcilhinney
Then you should be inheriting a common base class that includes that functionality and you can cast the ActiveMdiChild as that base type and call the common method regardless of what form it is.