|
-
Aug 22nd, 2005, 07:14 AM
#1
Thread Starter
Addicted Member
Tool Bar Methods
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?
Cute Member 
-
Aug 22nd, 2005, 06:43 PM
#2
Re: Tool Bar Methods
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.
-
Aug 23rd, 2005, 12:02 AM
#3
Thread Starter
Addicted Member
Cute Member 
-
Aug 23rd, 2005, 12:36 AM
#4
Re: Tool Bar Methods
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();
}
}
}
-
Aug 24th, 2005, 06:09 AM
#5
Thread Starter
Addicted Member
Re: Tool Bar Methods
 Originally Posted by jmcilhinney
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 repeat
VB Code:
if (activeChildType == typeof(ChildForm1))
{
((ChildForm1)this.ActiveMdiChild).DoSomething();
}
else if (activeChildType == typeof(ChildForm2))
{
((ChildForm2)this.ActiveMdiChild).DoSomething();
}
for 100 times / constructs?
Cute Member 
-
Aug 24th, 2005, 06:19 AM
#6
Re: Tool Bar Methods
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.
-
Aug 24th, 2005, 06:28 AM
#7
Thread Starter
Addicted Member
Re: Tool Bar Methods
 Originally Posted by jmcilhinney
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.
Cute Member 
-
Aug 24th, 2005, 07:46 AM
#8
Re: Tool Bar Methods
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|