Search code examples
c#excelvstoribboncountdowntimer

Create Countdown Timer for Excel Ribbon


I am looking to create a countdown timer for a ribbon in excel using VSTO when button1 is clicked.

Here is my code so far:

private void Ribbon1_Load(object sender, RibbonUIEventArgs e)
{
    TimerLabel.Label = "5:00";
    Convert.ToInt32(TimerLabel.Label);

}

private void button1_Click(object sender, RibbonControlEventArgs e)
{
    TimeSpan TimeDecrease = TimeSpan.FromSeconds(1);\
    TimerLabel.Label = Convert.ToInt32(TimerLabel.Label) - TimeDecrease;
}

}

Not to sure how to go about it. Any help is great


Solution

  • Here is the code for Ribbon.cs

        private TimeSpan startTimeSpan = new TimeSpan(0,5,0,0);
        private Timer timer = new Timer();
      public void Ribbon_Load(Office.IRibbonUI ribbonUI)
        {
            timer.Interval = 1000;
            this.ribbon = ribbonUI;
            timer.Tick += timer_Tick;
            timer.Start();
        }
    
        private void timer_Tick(object sender, EventArgs e)
        {
            TimeSpan timeDecrease = TimeSpan.FromSeconds(1);
            startTimeSpan = startTimeSpan - timeDecrease;
            ribbon.InvalidateControl("timerLabel");
        }
    
        public string timerLabel_getLabel(Office.IRibbonControl control)
        {
            return startTimeSpan.ToString();
        }
    
        //public void button1_onAction(Office.IRibbonControl control)
        //{
        //    timer.Start();
        //}
    

    Here is Ribbon.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <customUI onLoad="Ribbon_Load" xmlns="http://schemas.microsoft.com/office/2009/07/customui" >
        <ribbon>
            <tabs>
                <tab id="TimerTest" label="Timer">
                    <group id="group1" label="group1">
                        <labelControl id="timerLabel" getLabel="timerLabel_getLabel"/>
                        <button id="button1" label="button1" showImage="false" onAction="button1_onAction" />
                    </group>
                </tab>
            </tabs>
        </ribbon>
    </customUI>