Take the 2-minute tour ×

I have created an Add-In for Visual Studio 2008 that opens up a form, using Form1.Show(this);

If (while the form is open) the user opens/closes a Visual Studio dialog box, (such as Assembly Information) then the user cannot focus back on the form created by the addin.

Is there something I'm missing to allow the user to return to the form? This doesn't occur if I use Form1.ShowDialog(this), but I would like the user to see the Assembly Information while my custom form is open.

The add-in implements IWin32Window using

public System.IntPtr Handle
        return new System.IntPtr(_applicationObject.MainWindow.HWnd);

EDIT: Steps to reproduce

  • Create a visual studio add-in project
  • Add a reference to System.Windows.Forms
  • Add the following to public void Exec(...)

    System.Windows.Forms.Form f = new System.Windows.Forms.Form();

  • Run add-in, and open a project in the launched instance of visual studio
  • Open the project properties, go to the Application tab, open Assembly Information, and close it.
  • share|improve this question
    I tried to create simple add-in, but could not reproduce your problem. Could you please tell us more about your Form1? –  tia Sep 13 '10 at 19:02
    I've added the steps to reproduce. –  JamesMLV Sep 14 '10 at 14:19
    add comment

    2 Answers

    up vote 5 down vote accepted

    Thank you for the reproduction steps. I was able to reproduce your issue.

    As far as I was able to find out, the Visual Studio IDE uses Controls rather than Forms.

    Not knowing what the intended functionality is of your form I simply added a basic example below to get started.

    There could easily be many other ways of doing this. I'm no AddIn developer and as such my knowledge is limited in that area.

    The User Control

    First, right-click your project and add a new user control. I named mine "MyForm" in my example and placed a simple button on it, displaying "Hello" when clicked. This user control is going to be your form.

    namespace MyAddin1
        public partial class MyForm : UserControl
            public MyForm()
            private void button1_Click(object sender, EventArgs e)

    Creating the Form

    We need to use the application which is hosting your AddIn and the instance of your AddIn. Both those are members already declared in your AddIn project: _applicationObject and _addInInstance. Those are set in the OnConnection event.

    In the below code I create a new tool window, hosting my user control in it. I'm using the Windows2.CreateTooWindow2 Method to do that.

    I have added my sample code into the Excec event as seen below. Again, I'm not sure if that is the right place for it but for demonstrating the code it should suffice.

    /// <summary>Implements the Exec method of the IDTCommandTarget interface. This is called when the command is invoked.</summary>
    /// <param term='commandName'>The name of the command to execute.</param>
    /// <param term='executeOption'>Describes how the command should be run.</param>
    /// <param term='varIn'>Parameters passed from the caller to the command handler.</param>
    /// <param term='varOut'>Parameters passed from the command handler to the caller.</param>
    /// <param term='handled'>Informs the caller if the command was handled or not.</param>
    /// <seealso class='Exec' />
        object tempObject = null;   // It's required but I'm not sure what one can do with it...
        Windows2 windows2 = null;   // Reference to the window collection displayed in the application host.
        Assembly asm = null;        // The assembly containing the user control.
        Window myWindow = null;     // Will contain the reference of the new Tool Window.
            handled = false;
            if (executeOption == vsCommandExecOption.vsCommandExecOptionDoDefault)
                if (commandName == "MyAddin1.Connect.MyAddin1")
                    handled = true;
                    // Get a reference to the window collection displayed in the application host.
                    windows2 = (Windows2)_applicationObject.Windows;
                    // Get the executing assembly.
                    asm = Assembly.GetExecutingAssembly();
                    // Create the tool window and insert the user control.
                    myWindow = windows2.CreateToolWindow2(_addInInstance, asm.Location, "MyAddin1.MyForm", "My Tool Window", "{CB2AE2BD-2336-4615-B0A3-C55B9C7794C9}", ref tempObject);
                    // Set window properties to make it act more like a modless form.
                    myWindow.Linkable = false;  // Indicates whether the window can be docked with other windows in the IDE or not.
                    myWindow.IsFloating = true; // Indicates whether the window floats over other windows or not.
                    // Show the window.
                    myWindow.Visible = true;
        catch (Exception ex)

    I tested the application it did add my add-in to the tools menu of the IDE and when I clicked my Addin it showed the window and it worked. It also did not freeze, hang or anything when showing the Assembly Dialog.

    share|improve this answer
    add comment

    Perhaps these discussions might help?

    http://stackoverflow.com/questions/816981/can-i-set-showdialog-to-not-be-topmost http://stackoverflow.com/questions/1000847/how-to-get-the-handle-of-the-topmost-form-in-a-winform-app

    share|improve this answer
    add comment

    Your Answer


