Community
Showing results for 
Search instead for 
Do you mean 
Reply

How do you remove a toolbar button

Copper Contributor
Posts: 14
Country: USA

How do you remove a toolbar button

I created a toolbar button on the Opportunity Detail Toolbar.  But it stays around even when my plugin is not loaded (not in the plugin folder).

 

How do you remove the button?  I tried using the remove method in the unload event, but it does not remove the button from the toolbar.

 

An aditional question: Where is this toolbar information persisted? 

 

This is my code:

 

using Act.UI;
using Act.UI.Core;
using System.Windows;
using System.Windows.Forms;
using System.Drawing;
using System;

namespace SampleButtonPlugin
{
    class TabpageMenu : IPlugin
    {
        private ActApplication application;
        private bool buttonAdded = false;

        void IPlugin.OnLoad(ActApplication application)
        {
            //MessageBox.Show("Start of OnLoad");
            // hold on to the application
            this.application = application;

            // be sure to keep track of when a user logs in or is logged out
            //application.AfterLogon += new EventHandler(Application_AfterLogon);
            //application.BeforeLogoff += new EventHandler(Application_BeforeLogoff);
            application.ViewLoaded += new ViewEventHandler(actApp_ViewLoaded2);
            //MessageBox.Show("End of OnLoad");
        }

        void IPlugin.OnUnLoad()
        {
            //MessageBox.Show("Start of OnUnLoad");

            // this is equivalent to being disposed,
            // so make sure to detach from any events
            if (application != null)
            {
                if (buttonAdded)
                {
                    foreach (CommandBar mytBar in application.Explorer.CommandBarCollection)
                    {
                        //' Only add to toolbar ( Not Menus )
                        if (mytBar.Name.ToUpper().IndexOf("TOOLBAR") != -1)
                        {
                            if (mytBar.Name.ToUpper() == "Opportunity Detail Toolbar".ToUpper())
                            {
                                foreach (CommandBarControl myCBC in mytBar.ControlCollection)
                                {
                                    MessageBox.Show(myCBC.Text + "; " + myCBC.ControlType.ToString() + "; " + myCBC.Command);
                                }
                                CommandBarButton NewToolBarItem = new Act.UI.Core.CommandBarButton("myMenuText", "MyMenuToolTip", toolbarClick, "act-ui://com.act/application/menu/opportunity/sample1", Resource1.sample, Resource1.sample);
                                mytBar.ControlCollection.Remove(NewToolBarItem);
                            }
                        }
                    }
                }
                application.ViewLoaded -= new ViewEventHandler(actApp_ViewLoaded2);
                //application.AfterLogon -= new EventHandler(Application_AfterLogon);
                //application.BeforeLogoff -= new EventHandler(Application_BeforeLogoff);
                //ActFramework framework = application.ActFramework;
                //if (framework != null)
                //{
                //    framework.Database.BeforeDatabaseLock -= new Act.Framework.Database.DatabaseLockHandler(Database_BeforeDatabaseLock);
                //}
            }
        }

        protected void actApp_ViewLoaded2(object sender, Act.UI.ViewEventArgs e)
        {
            if (buttonAdded) return;
            MessageBox.Show("start of actApp_ViewLoaded2");
            //' Create a toolbar button
            CommandBarButton NewToolBarItem = new Act.UI.Core.CommandBarButton("myMenuText", "MyMenuToolTip", toolbarClick, "act-ui://com.act/application/menu/opportunity/sample1", Resource1.sample, Resource1.sample);
            //MessageBox.Show("item created actApp_ViewLoaded2");

            //' Add to all the toolbars available
            foreach (CommandBar mytBar in application.Explorer.CommandBarCollection)
            {
                //MessageBox.Show("collection Item actApp_ViewLoaded2");

                //' Only add to toolbar ( Not Menus )
                if (mytBar.Name.ToUpper().IndexOf("TOOLBAR") != -1)
                {
                    MessageBox.Show("found toolbar " + mytBar.Name.ToUpper());

                    if (mytBar.Name.ToUpper() == "Opportunity Detail Toolbar".ToUpper())
                    {
                        //foreach (CommandBarControl myCBC in mytBar.ControlCollection)
                        //{
                        //    MessageBox.Show(myCBC.Text + "; " + myCBC.ControlType.ToString() + "; " + myCBC.Command);
                        //}
                        //MessageBox.Show("Fount Opportunity toolbar actApp_ViewLoaded2");

                        try
                        {
                            mytBar.ControlCollection.Add(NewToolBarItem);
                            NewToolBarItem.IsVisible = false;
                            NewToolBarItem.IsVisible = true;
                            Application.DoEvents();

                            MessageBox.Show("ButtonAdded actApp_ViewLoaded2");
                            buttonAdded = true;
                            break;
                        }
                        catch(Exception myExeception)
                        {
                            MessageBox.Show("Error adding commandItem - " + myExeception.Message);
                        }
                    }
                }
            }
        }

 


        protected void actApp_ViewLoaded(object sender, Act.UI.ViewEventArgs e)
        {
            MessageBox.Show("Start of actApp_ViewLoaded");
            if (this.buttonAdded) return;

            //'the toolbar is not loaded until the IOpportunityDetailView form is displayed at startup
            //'so we load the button when we know the form has been loaded
            if (application.CurrentViewName == "Act.UI.IOpportunityDetailView")
            {

                MessageBox.Show("Adding Button actApp_ViewLoaded");
                Form view = application.CurrentView as Form;

                foreach (Control myControl in view.Controls)
                {
                    if (MessageBox.Show(myControl.Name, "Control Nmae", MessageBoxButtons.OKCancel) == DialogResult.Cancel) break;
                }

                Control[] ctls = view.Controls.Find("actionStrip", true);
                foreach (Control myControl in ctls)
                {
                    if (MessageBox.Show(myControl.Name, "Control Nmae", MessageBoxButtons.OKCancel) == DialogResult.Cancel) break;
                }
                ToolStrip s = ctls[0] as ToolStrip;

                ToolStripItem si;

                si = s.Items.Add("My Toolbar Item");
                si.Image = Resource1.sample;

                MessageBox.Show("Adding Click Event actApp_ViewLoaded");
                si.Click += new EventHandler(this.toolbarClick);

                buttonAdded = true;

                MessageBox.Show("End of actApp_ViewLoaded");
            }
        }

        protected void toolbarClick(object sender, EventArgs s)
        {
            MessageBox.Show("HI, from my toolbar item");
        }

    }
}

Employee
Posts: 1,163
Country: USA

Re: How do you remove a toolbar button

[ Edited ]

I believe this came from the sample included in the 2011 SDK, the first method removes an item from the tools menu, but isn't relevant to this thread so I didn't include it.

 

private void BeforeLogoff(object Sender, System.EventArgs e)
{
   RemoveMenuItem("act-ui://com.act/application/menu/tools/Test");
   RemoveIcon();
}

 

private void RemoveIcon()
{
    for(int i = 0; i < ActApp.Explorer.CommandBarCollection.Count; i++)
    {
        CommandBar cb = ActApp.Explorer.CommandBarCollection[i];
        cb.Dispose();
    }
}

Hope this helps.

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 14
Country: USA

Re: How do you remove a toolbar button

Thank You Matthew;

Thank You for your reply.  It helped, but I am still struggling with developing code for ACT!.

 

I must not have all the Act! 2011 SDK samples, because I searched all the documentation and sample files I have from the 2011 SDK ad could not find any  reference to RemoveMenuItem or RemoveIcon.  Can you point me to where the SDK samples are on the web?

 

Thank you

Mike

Employee
Posts: 1,163
Country: USA

Re: How do you remove a toolbar button

The SDK 2011 documentation can be found here.

 

Once you've downloaded and uzipped the contents the samples can be found here:  ..\Docs\Content\Code Samples

 

One of the samples is a custom control, the other is a menu item. There are some additional snippets in the htm pages, but these two .cs files can be added to an additional project and used as is.

Matthew Wood
Act! SDK Support
Community Moderator
Copper Contributor
Posts: 14
Country: USA

Re: How do you remove a toolbar button

Matthew;

Thanks,  I have those files.  I will reread them.  On question.  in the Sage ACT! 2011 SDK\Docs\Content\Code Samples\Introduction.htm file it says "

For an extended collection of samples maintained by Sage ACT! Engineering, see the ACT! Developer Network at http://actdevnet.communityserver.com."

 

But the link seems to be invalid.  Is this the correct link?  or do you have an updated link?

 

Mike

<script src="../SkinSupport/MadCapBodyEnd.js" type="text/javascript"></script>
Employee
Posts: 1,163
Country: USA

Re: How do you remove a toolbar button

Your correct, the link is no longer a valid URL. But fortunately you don't have far to go.

 

If you go up one level from this subforum you'll find the developer's download section which does contain additional samples.

 

http://community.act.com/t5/Sage-ACT-Developer-Downloads/bd-p/ADN_Downloads

 

 

Matthew Wood
Act! SDK Support
Community Moderator