为Excel功能区创建倒数计时器

时间:2014-01-24 09:22:16

标签: c# excel vsto ribbon countdowntimer

我想在点击button1时使用VSTO为excel中的功能区创建一个倒数计时器。

到目前为止,这是我的代码:

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;
}

}

不确定如何去做。任何帮助都很棒

1 个答案:

答案 0 :(得分:2)

以下是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();
    //}

这是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>