Shape cannot get focus after loss

I created an add-in for Visual Studio 2008 that opens a form using Form1.Show(this);

If (while the form is open) the user opens / closes the Visual Studio dialog box (for example, assembly information), then the user cannot focus on the form created by the add-in.

Is there something that I am missing to allow the user to return to the form? This does not happen if I use Form1.ShowDialog(this) , but I would like the user to see assembly information while my user form is open.

Add-in implements IWin32Window using

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

EDIT: steps to play

Create a visual studio add-in project Add a link to System.Windows.Forms Add the following to public void Exec(...) :
 System.Windows.Forms.Form f = new System.Windows.Forms.Form(); f.Show(); 

Launch the add-in and open the project in the running instance of visual studio Open the project properties, go to the Application tab, open Assembly Information and close it.
+4
source share
2 answers

Thank you for the playback steps. I was able to reproduce your problem.

As far as I can tell, the Visual Studio IDE uses controls, not forms.

Not knowing which function is designed for your form, I just added the basic example below to get started.

There can be many other ways to do this. I am not an AddIn developer, and therefore my knowledge is limited in this area.

User control

First, right-click the project and add a new user control. In my example, I named my "MyForm" and placed a simple button on it, showing "Hello" when clicked. This user control will be your form.

 namespace MyAddin1 { public partial class MyForm : UserControl { public MyForm() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Hello"); } } } 

Form creation

We need to use the application that hosts your AddIn and an instance of your AddIn. Both are members already declared in your AddIn project: _applicationObject and _addInInstance. They are set in the OnConnection event.

In the code below, I create a new tool window in which my user control is located. I am using the Windows2.CreateTooWindow2 method for this.

I added example code to the Excec event, as shown below. Again, I'm not sure if this is the right place for him, but it should be sufficient to demonstrate the code.

 /// <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' /> public void Exec(string commandName, vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { object tempObject = null; // It 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. try { 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; return; } } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

I tested an application that added my add-in to the IDE's tools menu, and when I clicked on my Addin, it showed a window and it worked. It also did not freeze, freeze or nothing when showing the Build Dialog.

+5
source

All Articles