Introduction
There is no resize event for custom task panes in Excel/Word/PowerPoint. If you want to resize your custom task panes using COM addin
in C# when the parent application (Excel/Word/PowerPoint) is resized i.e., maximized, minimized or windows resolution changed, you are at the correct place.
Background
The basics for creating COM addins in C# for MS Office applications is already available in CodeProject itself. So I assume that the following prerequisites
are met,
- A custom button is added to MS office ribbon
- A custom task pane is created after clicking the custom button
- Taskpane contains a user control which has the necessary controls as per your requirement.
Using the code
I have attached a sample MSI and source code for this sample. This example contains two user controls named MyUserControl
and MyUserControl2
.
First one is dummy control and the second one contains the sample controls like lable and textbox.
The ShowCustomTaskPane
function creates a custom task pane with MyUserControl
and adds MyUserControl2
dynamically.
The "Connect
" is derived from "ITaskPane
" interface which is used for interaction between Custom Taskpane and MyUserControl2
.
When MyUserControl2
loads, its controls' sizes are adjusted according to the TaskPane size. MyUserControl2
constructor stores the current resolution.
When the Office application is resized, MyUserControl2_Resize
function is called. This function is responsible for adjusting the controls size based
on resolution and scroll bar visibility.
private void MyUserControl2_Resize(object sender, EventArgs e)
{
if (m_IsCtrlLoading)
return;
if (this.Width > 300)
{
if (m_ObjItaskPane != null)
m_ObjItaskPane.SetTaskPaneDim(300, 0);
return;
}
if (!(sender is ScrollableControl))
return;
ScrollableControl scrllCtrl = sender as ScrollableControl;
if (IsResolutionChanged() || scrllCtrl.VerticalScroll.Visible == false)
AdjustUserControl();
}
If scrollbar in taskbar is visible, we no need to adjust the controls. If resolution is changed, adjusting the controls' size become mandatory.
The width of the TaskPane is kept constant because horizontal scroll bar will take care of the control's visibility in TaskPane.