Saving the state of a tool window
Hey,
I've created a simple tool window which I have attached my own Form to. When I close this form, I want it to be hidden the next time I open Visual Studio. I can get this functionality by simply not creating the tool window. It seems to be persistent the entire time (after open/close). The problem lies in that I have a Form I want attached to this window (I'm using the MS Shim control). I can get the Window from the list of windows but I'm unsure how to attach a new instance of my form to it. Before, I used the mToolWindow.CreateToolWindow which returned the last parameter which I could use to call HostUserControl.
Is there any way I can get access to this IVSUserControlHostCtl object?
Basically, I want this functionality:
if ( first_time_loading )
{
create tool window
show tool window
attach my custom view to the tool window
}
else if ( simply_reloading_visual_studio )
{
get existing tool window (my be hidden and I don't want to show it)
attach my custom view to the tool window
}
Is there any way I can do this? It's a pain for users to have to close my tool window every time they open Visual Studio. I'd rather have them be able to close it once and it'll stay closed.
The reason I need this is I need the view that's attached to the tool window so I can register events and what not.
Thanks
Shawn
I don't fully understand your scenario (when you say "attach a form" do you mean to host a .NET usercontrol, which is the typical use of the shim control?) So, some general info:
VS should be opened with the same windows that were visible when it was closed. If the user no longer requires a window, he can close it, the tool should not decide on his behalf. But if you really want to do it, the add-in can close its toolwindows when it is unloaded or it is loaded calling window.Visible=False.
When you call CreateToolwindow the last parameter returns the shim control. Given a Window already created you can also get this shim control through the Window.Object property. Maybe you were missing this, not sure.
An finally you have the source code of the shim control, so you can tweak it to meet your needs.
What I was wondering is if I create my tool window in the UISetup portion of my tool so that it only gets created once, is there a way of grabbing that upon a regular loading of my tool so that I can attach a custom form to it each time. Right now, I'm creating the tool window (the shim control) over and over again so that it always appears (since I have to set Visible = true).
If, upon a regular loading (not a UISetup), could I search for the tool window, grab the Window.Object (which you've said is the shim control), and call HostUserControl on that (after I check Visible == true)?
The UISetup phase is intended to create commands, that must be persisted even if the add-in is not loaded, to preserve keyboard bindings. Optionally, it can be used to create permanent toolbars/buttons/menus, to avoid the performance hit involved creating them each time. In this case, the toolbars/buttons/menus appear even if the add-in is not loaded (for that reason many of use don't like that approach); when the user clicks on a UI element, the add-in is actually loaded. But the UISetup phase is not intended to create toolwindows, AFAIK.
Toolwindows can be created either when the user invokes them, or when the add-in is loaded if you want to behave in such way. Once the toolwindow is invoked you naturally host the usercontrol and make the window visible.
When the user closes the toolwindow, it is actually hidden, not closed, so if he invokes it again you only have to make its window visible. This implies that you must have a "global" variable to hold the toolwindow, and when it is invoked through a command/button, if the variable is null you create and make it visible; otherwise it means that it was already created and you simply make it visible.
HTH