Click here to Skip to main content
12,396,230 members (66,045 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

9.2K views
551 downloads
3 bookmarked
Posted

Resize your custom task panes in MS Office applications

, 20 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This article briefly explains about resizing of custom task panes in Execl/Word/powerpoint

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,

  1. A custom button is added to MS office ribbon
  2. A custom task pane is created after clicking the custom button
  3. 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)
    {
        //Dont allow the width of the task pane to be modified. 
        if (m_ObjItaskPane != null)
            m_ObjItaskPane.SetTaskPaneDim(300, 0);
        return;
    }

    if (!(sender is ScrollableControl))
        return;

    // If Scroll bar exists, dont adjust the size of controls.
    // If resolution changed, you must adjust the controls 
    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. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Sivaji1565
Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 4 Pin
Amir Mohammad Nasrollahi20-Aug-13 6:20
professionalAmir Mohammad Nasrollahi20-Aug-13 6:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 20 Aug 2013
Article Copyright 2013 by Sivaji1565
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid