How to have a CheckBox checked by default when custom Ribbon is loaded.

1,569 views
Skip to first unread message

Kapil Sharma

unread,
Jan 23, 2014, 1:08:45 AM1/23/14
to exce...@googlegroups.com
Hi All,

I'm trying to create my first Excel AddIn and want to have a CheckBox which is checked when my custom Ribbon is loaded.

I think this can be done as it is shown here using VBA:- how-to-add-a-checkbox-to-the-ribbon-that-by-deafult-is-checked

I've everything working as needed EXCEPT this and I've spent a lot of time in getting this to work for my solution in C#, so any help is greatly appreciated.


Following is my Ribbon Class:

using System.Diagnostics;
using System.Runtime.InteropServices;
using ExcelDna.Integration;
using ExcelDna.Integration.CustomUI;
using MyRepository;
 
namespace My.Excel
{
    [ComVisible(true)]
    public  class Ribbon : ExcelRibbon, IExcelAddIn
    {
        private MyRepository _myRepository;
        private bool _suppressWarningsCheckBoxState;
 

public void AutoOpen()
        {
            _suppressWarningsCheckBoxState = true;
            _myRepository = new MyRepository();
            Functions.MyRepository = _myRepository;            
        }
 
        public void AutoClose()
        {
            //throw new NotImplementedException();
        }

 
        public override string GetCustomUI(string ribbonId)
        {
            return @"
                <customUI xmlns=""http://schemas.microsoft.com/office/2009/07/customui"" onLoad=""OnRibbonLoad"">
                  <ribbon>
                    <tabs>
                      <tab id=""TabPod"" label=""POD Tools"">
                        <group id=""Group1"" label=""Engineering Tools"">
                          <checkBox id=""CheckBoxAutoUpdate"" label=""Auto Update"" enabled=""true"" onAction=""OnAutoUpdateChanged""/>
                          <checkBox id=""CheckBoxSuppressWarnings"" label=""Suppress Warnings"" enabled=""true"" getPressed=""OnSuppressWarningsPressed"" onAction=""OnSuppressWarningsChanged""/>
                          <button id=""ButtonCustomDialog"" label=""Show Custom Dialog"" onAction=""OnButtonClicked""/>
                        </group>
                      </tab>
                    </tabs>
                  </ribbon>
                </customUI>";
        }
 
 
        public void OnRibbonLoad(IRibbonUI sender)
        {
            _suppressWarningsCheckBoxState = true;
            sender.InvalidateControl("CheckBoxSuppressWarnings");
        }
 
 
        public void OnSuppressWarningsPressed(IRibbonControl control,ref bool pressed)
        {
            pressed = _suppressWarningsCheckBoxState;
        }
 
        public void OnAutoUpdateChanged(IRibbonControl control, bool pressed)
        {
            Functions.SetAutoUpdate(pressed);
        }
 
        public void OnSuppressWarningsChanged(IRibbonControl control, bool pressed)
        {
            Functions.SetSuppressWarnings(!pressed);            
        }
 
        public void OnButtonClicked(IRibbonControl control)
        {
            Functions.CustomDialog();
        }
 
    }
}












Sergey Zhilyakov

unread,
Jan 23, 2014, 3:05:37 AM1/23/14
to exce...@googlegroups.com
Hi,

try the following callback function:

public bool OnSuppressWarningsPressed(IRibbonControl control)
{
    return _suppressWarningsCheckBoxState;
}


Best regards,
Sergey


--
You received this message because you are subscribed to the Google Groups "Excel-DNA" group.
To unsubscribe from this group and stop receiving emails from it, send an email to exceldna+u...@googlegroups.com.
To post to this group, send email to exce...@googlegroups.com.
Visit this group at http://groups.google.com/group/exceldna.
For more options, visit https://groups.google.com/groups/opt_out.

Govert van Drimmelen

unread,
Jan 23, 2014, 3:10:46 AM1/23/14
to exce...@googlegroups.com
Hi,

(That's very fast, Sergey!)

Sergey is right - the ribbon signatures are different for VBA and VB.NET. Your CheckBox getPressed is a function returning a Boolean.
You need not Invalidate the control in the onLoad - the getPressed will get called correctly the first time.

You can get a list of all the Ribbon callback signatures here: http://msdn.microsoft.com/en-us/library/aa722523(v=office.12).aspx
For VB.NET you need to look at the "Visual Basic:" signatures.

Regards,
Govert

Kapil Sharma

unread,
Jan 23, 2014, 9:00:27 AM1/23/14
to exce...@googlegroups.com
Thanks a lot Sergey and Govert

That was very easy and simple.... It worked..
Reply all
Reply to author
Forward
0 new messages