Andri Yadi

Butterfly Effect
See also: Other Geeks@INDC

Synchronizing Task Pane Visibility State and Toggle Button on Ribbon

Menjawab pertanyaan, keluhan, dan tantangan dari sdr Dondy :) yang terungkap pada saat ngobrol-ngobrol di acara Heroes Gathering (2 Agustus 2008, di Gunung Putri Bogor), gw posting tulisan ini.

You know Task Pane in MS Office products right (refer to picture below)? The question is how to control task pane visibility from a toggle button on ribbon. If the toggle button is checked (bevel in), the task pane is visible, and vice versa. What if the task pane is closed by clicking "x" button on it? Then toggle button state must reflect that closing by showing unchecked state. So, the problem is how to sync task pane visibility state with toggle button checked state. How can I do that using VSTO?

image

Toggle button is checked --> Task Pane is visible

image

Toggle button is unchecked --> Task Pane is not visible

Here is the workaround using VSTO v3. In this example, I use project template Excel 2007 Add-in.

1. Create new project Excel 2007 Add-in, name the project whatever you want

2. Add new Ribbon (Visual Designer), name it whatever, lets say MyRibbon. When Ribbon visual designer is appeared, drag Toggle Button control into the ribbon. Set toggle button's Label to "Task Pane", and set its name to "toggleButton_TaskPane". The result is:

image

3. Code the ribbon.

using System;
using Microsoft.Office.Tools.Ribbon;
 
namespace ExcelCustomTaskPane
{
    public partial class MyRibbon : OfficeRibbon
    {
        public MyRibbon()
        {
            InitializeComponent();
        }
 
        private void MyRibbon_Load(object sender, RibbonUIEventArgs e)
        {
            IsTaskPaneVisible = Globals.ThisAddIn.ctp.Visible;
        }
 
        private bool isTaskPaneVisible;
        public bool IsTaskPaneVisible
        {
            get
            {
                return isTaskPaneVisible;
            }
            set {
                isTaskPaneVisible = value;
                toggleButton_TaskPane.Checked = value;
            }
        }
 
        
    }
}

3. Add a User Control to as a content for Task Pane, name it MyTaskPane.cs. For simplicity, I just drag DateTimePicker control into that user control.

image

4. Find ThisAddIn.cs in solution explorer, open it, and write this code:

using System;
using Microsoft.Office.Tools;
 
namespace ExcelCustomTaskPane
{
    public partial class ThisAddIn
    {
        private MyRibbon ribbon;
        internal CustomTaskPane ctp;
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            ribbon = Globals.Ribbons.MyRibbon;
            ctp = this.CustomTaskPanes.Add(new MyTaskPane(), "My Task Pane");
            ctp.Visible = true;
            ctp.VisibleChanged += new EventHandler(ctp_VisibleChanged);
        }
 
        void ctp_VisibleChanged(object sender, EventArgs e)
        {
            ribbon.IsTaskPaneVisible = ctp.Visible;
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
 
    }
}

5. Back to our Ribbon. Double click to Toggle Button and write code to handle its click event:

private void toggleButton_TaskPane_Click(object sender, RibbonControlEventArgs e)

        {
            Globals.ThisAddIn.ctp.Visible = toggleButton_TaskPane.Checked;
        }

Globals.ThisAddIn.ctp refers to ctp field in ThisAddIn class, which is an instance of CustomTaskPane.

That's it. Now, you can run the project and see how it works.

The key here is handling VisibleChanged event of CustomTaskPane. When Task Pane (referred as ctp object in our code) visibility state is changed (visible become not visible, and vice versa), that state should influence toggle button state. On the other hand, toggle button state should influence task pane state.

If you use Excel 2007 Document or Template project (you are developing Document Level Customization in VSTO), working with Task Pane can be done by using ActionsPane object. Unfortunately, you can't capture VisibleChanged event of ActionsPane (even the event is available). And I still can't figure it out.

That's it. Find the attachment for complete solution.

Dondy, gitu kan pertanyaan lu? Kalau bukan, lets discuss it further. Kalau udah solved, berarti Sony DSLR A-300x-nya bisa turun harga lagi dong :) Good luck.

Share this post: | | | |
Posted: Aug 04 2008, 04:56 PM by andriyadi | with 3 comment(s) |
Filed under:

Comments

dondy said:

Hahahah thanks...

method ini  void ctp_VisibleChanged(object sender, EventArgs e) dapat dari mane ?

# August 4, 2008 5:14 PM

andriyadi said:

@dondy

Method itu untuk handle event VisibleChanged dari CustomTaskPane. Lihat statement:

ctp.VisibleChanged += new EventHandler(ctp_VisibleChanged);

Kalo lu nanya event VisibleChanged dari mana? ya dari sononya udah ada bos :)

Good luck

# August 4, 2008 5:50 PM

dondy said:

Tau aje lu gw bakal nanya itu... documentnya ada dimana Ndri ? MSDN ada kah ?

# August 4, 2008 9:50 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 
Are you human?:  


Enter the numbers above: