views:

1377

answers:

4

I wrote a simple add-in for Visual Studio 2008 that opens a dockable window pane.

You can download the source and a binary installer by clicking here.

The nature of the add-in means that it is ideally going to stay docked next to where you edit your source. But sometimes, on some installs, it won't stay docked. You run VS, you dock my pane, you shutdown VS, you restart VS, and dang it - the pane is floating again. On some machines I have to re-dock it every time.

But on other installs it stays docked wherever I put it forever. I originally thought it might be a difference between Vista and XP but now I have reports of it coming unstuck on XP as well.

From what I've read (and the fact that it sometimes stays docked) I get the impression that VS is supposed to take care of saving the docking state for me. But it isn't doing that. And yet other plugins on the same VS install don't have this problem. So there has to be something I can do to improve the situation.

I suspect the only relevant part of my code is this:

public class Connect : IDTExtensibility2
{
    private static DTE2 _applicationObject;
    private AddIn _addInInstance;
    private static CodeModelEvents _codeModelEvents;

    public static DTE2 VisualStudioApplication
    {
        get { return _applicationObject; }
    }

    public static CodeModelEvents CodeModelEvents
    {
        get { return _codeModelEvents; }
    }

    public static event EventHandler SourceChanged = delegate { };

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

 public void OnStartupComplete(ref Array custom)
 {
        try
        {
            Events2 events = (Events2)_applicationObject.Events;
            _codeModelEvents = events.get_CodeModelEvents(null);

            object objTemp = null;

            Windows2 toolWins = (Windows2)_applicationObject.Windows;

            Window toolWin = toolWins.CreateToolWindow2(
                _addInInstance, GetType().Assembly.Location, "Ora.OraPane", "Ora", 
                "{DC8A399C-D9B3-40f9-90E2-EAA16F0FBF94}", ref objTemp);
            toolWin.Visible = true;
        }
        catch (Exception ex)
        {
            MessageBox.Show("Exception: " + ex.Message);
        }
    }

 public void OnBeginShutdown(ref Array custom) { }

    public void OnDisconnection(ext_DisconnectMode disconnectMode, ref Array custom) { }

    public void OnAddInsUpdate(ref Array custom) { }
}

(The MSDN docs suggest that the window should be created in OnConnection, but if I do that then the window mostly doesn't appear.)

+8  A: 

Hi - I faced the some of the same docking issues with TeamReview (http://www.codeplex.com/TeamReview). I can't say why it happens but I can help point you to code that always docs your window in OnStartupComplete. If you have a particular location you want it to be in you can Link it to be inside a frame after creating your toolWin and before calling the Visible property. You'll have to check into which constants fit your condition for the CreateLinkedWindowFrame, and SetKind methods. Also, you may want to link your window to something other than MainWindow such as the SolutionExplorer

EnvDTE80.Window2 frame = toolWins.CreateLinkedWindowFrame(toolWin, toolWin, vsLinkedWindowType.vsLinkedWindowTypeTabbed);


frame.SetKind(EnvDTE.vsWindowType.vsWindowTypeToolWindow);


_applicationObject.MainWindow.LinkedWindows.Add(frame);

frame.Activate();

This sample is similar to: http://www.codeplex.com/TeamReview/SourceControl/changeset/view/16102# 2008 -> TeamReview -> Command -> ShowReplayWindowCommand.cs -> ShowForm()

Here's a good Microsoft example to link together the Output Window, the Command Window, and Solution Explorer. It then manipulates the width and height of these linked windows, and it finally undocks them all from the linked window frame.

JB Brown
Thanks for such a detailed helpful answer. What I really want is for my pane to stay exactly where the user put it, but I guess I'll have to hack something together that approximates it.
Daniel Earwicker
Developing an addin here. This post was TOTALLY helpful. Would have voted twice if it didn't remove my vote instead :P.
Maxim
A: 

I have the same problem as the author. I noticed that Visual Studio 2005 "forgets" tool window position only after sessions in which debugging was used.

The accepted answer doesn't help much because the tool window always gets docked to the bottom. I really want the users to be able choose where they want to dock and save their preference by simply docking where they like.

Juozas Kontvainis
I agree. If someone has a better suggestion, I'll accept it instead!
Daniel Earwicker
A: 

Here's what helped for me. I use Visual Studio 2005, but this might help you, too.

public void OnBeginShutdown(ref Array custom)
{
     if (_toolWin != null)
          _toolWin.Visible = false;
}
Juozas Kontvainis
I tried that but it made no difference.
Daniel Earwicker
Have you noticed any patterns when your tool window gets undocked? For me the problem was debugging, i.e. if I started Visual Studio and didn't start debugging, my addin tool window worked OK, if I debugged something - it would be undocked in the next session.
Juozas Kontvainis
A: 

I am pretty new to Visual Studio but am having the same problem and I can't believe Microsoft hasn't addressed this issue if so many people are having the same problem.

So where do I place the above method? I have no idea how to implement this.

Thanks for the help Jesse