none
How to get the checked value of a checkBox for a custom tab in C#? RRS feed

  • Question

  • Hi, friends

    I have a custom tab for Excel2010 in C#.

    First, i write the xml markup file as follow:

    ......

    <checkBox id="checkBox1" label="checkBox1" getPressed="checkBox1_getPressed"

    onAction="checkBox1_click" />

    ......

    And then i write its callbacks as follow:

    ......

      public bool checkBox1_getPressed(Office.IRibbonControl control)
            {
                bool midBool = false;
                Excel.Application xlApp = new Excel.Application();
                xlApp = Globals.ThisAddIn.Application;
                midBool = (xlApp.ActiveWindow.DisplayZeros == true ? true : false);
                return midBool;
            }
            public void checkBox1_click(Office.IRibbonControl control)
            {

                bool pressed=false;          

               Excel.Application xlApp = new Excel.Application();        

               xlApp = Globals.ThisAddIn.Application;

               pressed=?????     //How to get the checked value of "checkBox1"?

                switch (pressed)
                {
                    case (true):
                        xlApp.ActiveWindow.DisplayZeros = true;
                        break;
                    default:
                        xlApp.ActiveWindow.DisplayZeros = false;
                        break;
                }
            }

    ......

    I want to get the result as follows:

    when the checked value of "checkBox1" is true, set "xlApp.ActiveWindow.DisplayZeros" as "true",otherwise set "xlApp.ActiveWindow.DisplayZeros" as "false".

    But i don't know how to get the checked value of "checkBox1".

    Please help me,thanks!



    • Edited by hopedream Monday, May 12, 2014 6:59 AM
    Monday, May 12, 2014 6:54 AM

Answers

  • Hello,

    First of all, there is no need to create a new Application instance in the callbacks:

    Excel.Application xlApp = new Excel.Application();        

    If you develop a VSTO based add-in you can use the Application property provided by the VSTO base classes.

    You need to specify the getPressed callback if you want to know the pressed state of the check box control:

    public bool pressedState = false;
    public bool GetPressed(IRibbonControl control)
    {
        return pressedState;
    }
    

    You can read more about this in the Adding Custom Check Boxes to the 2007 Office Fluent User Interface article in MSDN.

    Monday, May 12, 2014 10:21 AM
  • Hi,

    I notice that the callback of checkbox is incorrect, you can get the value of checkbox when you click it like below:

     public void checkBox1_click(Office.IRibbonControl control, bool pressed)
            {
               
                Excel.Application xlApp = Globals.ThisAddIn.Application;
                switch (pressed)
                {
                    case (true):
                        xlApp.ActiveWindow.DisplayZeros = true;
                        break;
                    default:
                        xlApp.ActiveWindow.DisplayZeros = false;
                        break;
                }
            }

    You can get more detail from link Eugene metioned:
    Adding Custom Check Boxes to the 2007 Office Fluent User Interface

    >>I still have a quetion how to  change the pressed state of the check box control  when the worksheet or workbook is changed.<<

    An easy way to achieve this goals is using Ribbon(Visual Designer not xml), you can get the control like code below:

      void Application_SheetChange(object Sh, Excel.Range Target)
            {        
                Globals.Ribbons.Ribbon1.checkBox1.Checked = !Globals.Ribbons.Ribbon1.checkBox1.Checked;
            }

    Hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, May 13, 2014 5:58 AM
    Moderator

All replies

  • Hello,

    First of all, there is no need to create a new Application instance in the callbacks:

    Excel.Application xlApp = new Excel.Application();        

    If you develop a VSTO based add-in you can use the Application property provided by the VSTO base classes.

    You need to specify the getPressed callback if you want to know the pressed state of the check box control:

    public bool pressedState = false;
    public bool GetPressed(IRibbonControl control)
    {
        return pressedState;
    }
    

    You can read more about this in the Adding Custom Check Boxes to the 2007 Office Fluent User Interface article in MSDN.

    Monday, May 12, 2014 10:21 AM
  • I still have a quetion how to  change the pressed state of the check box control  when the worksheet or workbook is changed.
    Tuesday, May 13, 2014 1:46 AM
  • Hi,

    I notice that the callback of checkbox is incorrect, you can get the value of checkbox when you click it like below:

     public void checkBox1_click(Office.IRibbonControl control, bool pressed)
            {
               
                Excel.Application xlApp = Globals.ThisAddIn.Application;
                switch (pressed)
                {
                    case (true):
                        xlApp.ActiveWindow.DisplayZeros = true;
                        break;
                    default:
                        xlApp.ActiveWindow.DisplayZeros = false;
                        break;
                }
            }

    You can get more detail from link Eugene metioned:
    Adding Custom Check Boxes to the 2007 Office Fluent User Interface

    >>I still have a quetion how to  change the pressed state of the check box control  when the worksheet or workbook is changed.<<

    An easy way to achieve this goals is using Ribbon(Visual Designer not xml), you can get the control like code below:

      void Application_SheetChange(object Sh, Excel.Range Target)
            {        
                Globals.Ribbons.Ribbon1.checkBox1.Checked = !Globals.Ribbons.Ribbon1.checkBox1.Checked;
            }

    Hope it is helpful.

    Best regards

    Fei


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.




    Tuesday, May 13, 2014 5:58 AM
    Moderator