With VSTO 2005 adding controls to the ActionsPane is pretty simple compared
to what you had to do in the past. Calling Globals.ThisDocument.ActionsPane.Controls.Add(...)
gets the job done nicely and without hassle. Typically you will build a user
control with all of the functionality that is desired from
the "Document Actions" pane and add that as a whole to Globals.ThisDocument.ActionsPane.
In such cases you will want the user control you added to occupy the whole
of the "Document Actions" pane and the most obvious solution is to
use Dock.Fill. Unfortunately this only works to a certain extent;
your control will be correctly resized horizontally, but not vertically.
Using the Anchor property does not help either, in fact it has exactly
the same behavior as Dock. I am sure there is a reasonable explanation
behind this, but I don't know what it is; however, I do know how to
work around it (not that it took too much brain power, it is pretty simple).
First add a method that will be responsible for resizing your control
residing in the document action's pane. It will look something like the
following
/// <summary>
/// Resizes the Action Pane's child control
/// (assumes there is always only one)
/// as Word does not do this automaticaly.
/// </summary>
private void ResizeActionsPaneChildControl()
{
// If the actions pane has a control added to it
if (this.ActionsPane.Controls.Count > 0)
{
// Set the control's height to be the same
// height as the action pane (manual resize)
// There will always be only one child
// control added, so using [0] is fine here.
this.ActionsPane.Controls[0].Height =
this.ActionsPane.Height - 10;
}
}
You might have noticed that the code above includes a dodgy looking -10
in the assignment of the child control's height. That is because I have found
this value to be typical of the margin around the "Document Actions"
pane; if I do not reduce the height by 10 pixels I always end up with a
vertical scrollbar.
Now you will need to invoke this resizing method from two places
- After
the line of code that adds the control to the actions pane (e.g. Globals.ThisDocument.ActionsPane.Controls.Add(customDocumentActions))
- The
event handler of the Globals.ThisDocument.ActionsPane.Resize
event
Be careful of the order you do things. Either be flexible in your resizing
method (like I have been above) and expect that sometimes the
"Document Actions" pane will be empty, or first add the user control
to the "Document Actions" pane, then call the resizing method and
then start listening to the Globals.ThisDocument.ActionsPane.Resize
event.
Important note: Always ensure your control is usable when the window it
lives in is resized to very small width and height. If necessary use the
MinimumSize property to force the use of scrollbars after a certain size
(rather than squashing all your child controls up).