Results 1 to 8 of 8

Thread: Tool Bar Methods

  1. #1

    Thread Starter
    Addicted Member cutamacious's Avatar
    Join Date
    May 2001
    Location
    INDIA >> Andhra Pradesh >> Hyderabad
    Posts
    185

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member cutamacious's Avatar
    Join Date
    May 2001
    Location
    INDIA >> Andhra Pradesh >> Hyderabad
    Posts
    185

    Thumbs down Re: Tool Bar Methods

    Quote Originally Posted by jmcilhinney
    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.


    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.
    Cute Member

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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();
    		}
    	}
    }
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member cutamacious's Avatar
    Join Date
    May 2001
    Location
    INDIA >> Andhra Pradesh >> Hyderabad
    Posts
    185

    Re: Tool Bar Methods

    Quote 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:
    1. if (activeChildType == typeof(ChildForm1))
    2.         {
    3.             ((ChildForm1)this.ActiveMdiChild).DoSomething();
    4.         }
    5.         else if (activeChildType == typeof(ChildForm2))
    6.         {
    7.             ((ChildForm2)this.ActiveMdiChild).DoSomething();
    8.         }

    for 100 times / constructs?
    Cute Member

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member cutamacious's Avatar
    Join Date
    May 2001
    Location
    INDIA >> Andhra Pradesh >> Hyderabad
    Posts
    185

    Re: Tool Bar Methods

    Quote 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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width