Quantcast
Channel: Visual Studio Integrate forum
Viewing all articles
Browse latest Browse all 4410

Problem in adding two buttons in two diffrent locations in my VS AddIn

$
0
0

I need to add two buttons for two purposes in my add in's _applicationObject's menu bars. one in the solution explorer'sWeb Item menu (to select a web page and do a task) and the other to the Standard tool bar (to click and view a tool window that I'v created)

before adding code to add the button in Standard tool bar, I could get the button under solution explorer by right clicking a web page but after adding another button to the standard tool bar, now I can view the secondly added button only. 

can anyone please help me, where in the below code I am doing wrong...

	public class Connect : IDTExtensibility2, IDTCommandTarget
	{
        private DTE2 _applicationObject;
        private AddIn _addInInstance;
        private static string selectedFile;

        private const int TOOLWINDOW_INVISIBLE = 0;
        private const int TOOLWINDOW_VISIBLE = 1;

        private const string COMMAND_NAME = "UsabilityEvaluationCommand";
        private const string COMMAND_CAPTION = "Evaluate Usability";
        private const string COMMAND_TOOLTIP = "Show the toolwindow of the add-in";

        private CommandBarButton standardCommandBarButton;
        private EnvDTE.Window toolWindow;

		public Connect()
		{
		}
		
        public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
        {
            try
            {
                _applicationObject = (DTE2)application;
                _addInInstance = (AddIn)addInInst;           

                if (connectMode == ext_ConnectMode.ext_cm_UISetup)
                {
                    object[] contextGUIDS = new object[] { };
                    Commands2 commands = (Commands2)_applicationObject.Commands;

                    //Place the command on the tools menu.
                    //Find the MenuBar command bar, which is the top-level command bar holding all the main menu items:

                    CommandBar standardToolBar2 = ((Microsoft.VisualStudio.CommandBars.CommandBars)_applicationObject.CommandBars)["Web Item"];

                    //Add a command to the Commands collection:
                    Command command = commands.AddNamedCommand2(_addInInstance, "UsabilityEvaluationAddIn", "Check Usability", "Click to start evaluating usability of this page", true, 2131, ref contextGUIDS, (int)vsCommandStatus.vsCommandStatusSupported + (int)vsCommandStatus.vsCommandStatusEnabled, (int)vsCommandStyle.vsCommandStylePictAndText, vsCommandControlType.vsCommandControlTypeButton);

                    //Add a control for the command to the tools menu:
                    if ((command != null) && (standardToolBar2 != null))
                    {
                        CommandBarControl ctrl2 = (CommandBarControl)command.AddControl(standardToolBar2, 3);
                        ctrl2.TooltipText = "Click to start evaluating usability of this page";
                    }
                }

                switch (connectMode)
                {
                    case ext_ConnectMode.ext_cm_Startup:

                        // The add-in was marked to load on startup
                        // Do nothing at this point because the IDE may not be fully initialized
                        // Visual Studio will call OnStartupComplete when fully initialized
                        break;

                    case ext_ConnectMode.ext_cm_AfterStartup:

                        // The add-in was loaded by hand after startup using the Add-In Manager
                        // Initialize it in the same way that when is loaded on startup
                        AddTemporaryUI();
                        break;
                }
            }      
                catch (System.ArgumentException)
                {
                    //If we are here, then the exception is probably because a command with that name
                    //  already exists. If so there is no need to recreate the command and we can 
                    //  safely ignore the exception.
                }

                catch (System.Exception e)
                {
                    System.Windows.Forms.MessageBox.Show(e.ToString());
                }
        }
       
        public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom)
        {
            Microsoft.Win32.RegistryKey registryKey;
            int myToolWindowVisible;

            try
            {
                switch (disconnectMode)
                {
                    case ext_DisconnectMode.ext_dm_HostShutdown:
                    case ext_DisconnectMode.ext_dm_UserClosed:

                        if ((standardCommandBarButton != null))
                        {
                            standardCommandBarButton.Delete(true);
                        }

                        // Store in the Windows Registry if the toolwindow was visible when unloading the add-in
                        myToolWindowVisible = TOOLWINDOW_INVISIBLE;

                        if (toolWindow != null)
                        {
                            if (toolWindow.Visible)
                            {
                                myToolWindowVisible = TOOLWINDOW_VISIBLE;
                            }
                        }
                        registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\MyToolWindow");
                        registryKey.SetValue("MyToolwindowVisible", myToolWindowVisible);
                        registryKey.Close();

                        break;
                }
            }
            catch (System.Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }
      		
        public void OnAddInsUpdate(ref Array custom)
        {
        }
       
        public void OnStartupComplete(ref Array custom)
        {
           AddTemporaryUI();
        }

        public void AddTemporaryUI()
        {
            const string VS_STANDARD_COMMANDBAR_NAME = "Standard";

            Command myCommand = null;
            CommandBar standardCommandBar = null;
            CommandBars commandBars = null;
            Microsoft.Win32.RegistryKey registryKey;

            object[] contextUIGuids = new object[] { };

            try
            {
                // Try to retrieve the command, just in case it was already created, ignoring the 
                // exception that would happen if the command was not created yet.
                try
                {
                    myCommand = _applicationObject.Commands.Item(_addInInstance.ProgID + "." + COMMAND_NAME, -1);
                }
                catch
                {
                }

                // Add the command if it does not exist
                if (myCommand == null)
                {
                    myCommand = _applicationObject.Commands.AddNamedCommand(_addInInstance,
                       COMMAND_NAME, COMMAND_CAPTION, COMMAND_TOOLTIP, true, 2131, ref contextUIGuids,
                       (int)(vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled));
                }

                // Retrieve the collection of commandbars
                commandBars = (CommandBars)_applicationObject.CommandBars;

                // Retrieve some built-in commandbars
                standardCommandBar = commandBars[VS_STANDARD_COMMANDBAR_NAME];

                // Add a button on the "Standard" toolbar
                standardCommandBarButton = (CommandBarButton)myCommand.AddControl(standardCommandBar,
                   standardCommandBar.Controls.Count + 1);

                // Change some button properties
                standardCommandBarButton.Caption = COMMAND_CAPTION;
                standardCommandBarButton.BeginGroup = true;

                // Get if the toolwindow was visible when the add-in was unloaded last time to show it
                registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\MyToolWindow");
                if (registryKey != null)
                {
                    if ((int)registryKey.GetValue("MyToolwindowVisible") == TOOLWINDOW_VISIBLE)
                    {
                        ShowToolWindow();                       
                    }
                    registryKey.Close();
                }
            }
            catch (System.Exception e)
            {
                System.Windows.Forms.MessageBox.Show(e.ToString());
            }
        }
        public void OnBeginShutdown(ref Array custom)
        {          
        }

        public void QueryStatus(string commandName, vsCommandStatusTextWanted neededText, ref vsCommandStatus status, ref object commandText)
        {
            if (neededText == vsCommandStatusTextWanted.vsCommandStatusTextWantedNone)
            {
                if (commandName == _addInInstance.ProgID + "." + COMMAND_NAME)
                {
                    status = (vsCommandStatus)vsCommandStatus.vsCommandStatusSupported | vsCommandStatus.vsCommandStatusEnabled;
                    return;
                }
                else
                {
                    status = vsCommandStatus.vsCommandStatusUnsupported;
                }
            }
        }

        private void ShowToolWindow()
      { 
         const string TOOLWINDOW_GUID = "{6CCD0EE9-20DB-4636-9149-665A958D8A9A}";

         EnvDTE80.Windows2 windows2;
         string assembly;
         object myUserControlObject = null;
         DeviationsToolWindow myUserControl;

         try
         {
            if (toolWindow == null) // First time, create it
            {
               windows2 = (EnvDTE80.Windows2) _applicationObject.Windows;

               assembly = System.Reflection.Assembly.GetExecutingAssembly().Location;

               toolWindow = windows2.CreateToolWindow2(_addInInstance, assembly, 
                  typeof(DeviationsToolWindow).FullName, "Ditected Usability Issues", TOOLWINDOW_GUID, ref myUserControlObject);

               myUserControl = (DeviationsToolWindow) myUserControlObject;

            }

            toolWindow.Visible = true;
         }
         catch (System.Exception e)
         {
            System.Windows.Forms.MessageBox.Show(e.ToString());
         }

      }
          
        public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled)
        {
            handled = false;

            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
            {
                if (commandName == _addInInstance.ProgID + "." + COMMAND_NAME)
                {
                    handled = true;
                    selectedFile = GetSourceFilePath();
                    String message = "";

                    //if the selected file in a web page
                    //then, allow the user with the usability evaluation process
                    if (GetSourceFilePath().EndsWith(".aspx"))
                    {
                        Form messageDialog=new Form();
                        message = "Are you Sure to start Usability Evaluation on this page";
                        DialogResult result = System.Windows.Forms.MessageBox.Show(message, "Usabilty Tool Confirmation", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                        if (result == DialogResult.OK)
                        {
                            // call this method to get the source code of the selected file
                            GetSourceCodeOfWebPage();
                        }                       
                    }

                    //if not, display an error message
                    else
                    {                       
                        message = "You cannot Evaluate this page for Usability. Please select web pages only";
                        System.Windows.Forms.MessageBox.Show(message, "Usabilty Tool Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    ShowToolWindow();

                    return;
                }

            }
        }
   


Viewing all articles
Browse latest Browse all 4410

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>