What is the โ€œrightโ€ way to get a link to a tape object?

For a VSTO workbook project, is there a best practice for getting a reference to a Ribbon object from the ThisWorkbook class?

Here's what I do: in the Ribbon class, I created a public method called InvalidateControl(string controlID). I need to call this method from the ThisWorkbook class, based on when a particular workbook-level event fires. But the only way to see a โ€œlinkโ€ to this Ribbon object is to do this ...

    // This is all in the ThisWorkbook class
    Ribbon ribbon;
    protected override IRibbonExtensibility CreateRibbonExtensibilityObject()
    {
        this.ribbon = new Ribbon();
        return this.ribbon;
    }

... which seems a little smelly. I mean, I have to redefine CreateRibbonExtensibilityObject()independently; everything that I do, except that the local link to the tape is supported, so I can call methods against it. But this is not so. Is there any other, better way to get this link in the ThisWorkbook class? Or is it pretty acceptable?

Thank!

+3
source share
2 answers

An easier way is to create a global static variable somewhere (for example, in this book).

public static Ribbon ribbonref;

Then, in the Ribbon class code in the event handler for initialization (I think the method is called Ribbon1_StartUp(), but I'm not sure), set the variable:

private void Ribbon1_StartUp(object sender, EventArg e)
{
    ThisWorkbook.ribbonref = this;
}

(written from memory, so it may not be quite right)

ribbonref .

+2

. MSDN, Globals:

Globals.Ribbons.MyRibbon.MyObject.Text = "test";
0

Source: https://habr.com/ru/post/1783771/


All Articles