DCSIMG
February 2007 - Posts - IronShay

February 2007 - Posts

Creating a Toggle Button with Office 2003

Office 2007 has brought us the toggle button control, which was missing as an out-of-the-box control in Office 2003.

There is a nice workaround though, and this is what I'm going to talk about on this post.
A toggle button is a button that when it's clicked, it stays pressed until another click is made upon it. The most familiar toggle button I guess, is the bold button.
When you're in "bold text mode", the button looks like that ==> , and when you work normally, the button looks like that ==> .

This is a toggle button... The way to achieve this kind of behavior in Office 2003, is as follows:

1. Create a new button and register to its Click event:
(I'm adding the button to a command bar I've created and stored in myCommandBar variable. This can be done on every command bar of course...)

CommandBarButton btn = (CommandBarButton)myCommandBar.Controls.Add(
MsoControlType.msoControlButton, missing, missing, missing,
true); btn.Caption = "Click Me!"; btn.Style = MsoButtonStyle.msoButtonCaption; btn.Click += new _CommandBarButtonEvents_ClickEventHandler(btn_Click);

2. In the Click event handler, we'll set the State property of the control, to be pressed or released:

void btn_Click(CommandBarButton Ctrl, ref bool CancelDefault)
{
    Ctrl.State = (Ctrl.State == MsoButtonState.msoButtonDown) ? 
                    MsoButtonState.msoButtonUp : MsoButtonState.msoButtonDown;
    /*
        Do something...
    */
}

And now we have a toggle button in Office 2003.
Hip hip hooray!
Shay.

Posted by shayf | with no comments
תגים:, ,