Togglebutton state remains across inspectors

Add-in Express™ Support Service
That's what is more important than anything else

Togglebutton state remains across inspectors
 
Bert Sinnema


Guest


Hi All,

I just moved from traditional VSTO programming to using Add-in Express for .NET. I'm already loving the whole fact of being able to have a single codebase for my project. This will make maintenance and installation so much easier for me and my customers.

However before I will rewrite our Outlook product I want to play around a bit. I want to get a bit of a feel of the differences and of course i want to use Add-in Express as it was meant to be used.

My challenge:
Obviously I started out with some GUI stuff first, because we just like to see stuff shine, don't we.

So, I have this Togglebutton that, for now, just shows me a messagebox with a message. The message is, as i want, only displayed when the button is toggled on, very simple. But when I leave it toggled and I open a new Inspector, the state of the button in the new Inspector is the same, where it hould have the default value of not toggled.

The thing I am wondering about is: Does each Inspector (let's say mailmessage) have a seperate instance of the Ribbon or is it somehow inherited? For my "traditional" project I wrote a wrapper that i hooked up to the NewInspector event. But I would like to know if that's the way to go here or if there is a solution built into Add-in Express.

Best regards
Posted 16 Apr, 2015 05:12:16 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Bert,

Thank you for the nice words ))

As to the button state being the same in all inspectors, this is the result of using the default schema of updating Ribbon controls. When this update schema is used, the Ribbon component caches the property value and supplies it to the Ribbon whenever it requests that property. To have different values of the same property in different Inspector windows, you need to handle the PropertyChanging event of that button, please find details in section Updating Ribbon Controls at Run Time, see the PDF file in the folder {Add-in Express}\Docs on your development PC.


Andrei Smolin
Add-in Express Team Leader
Posted 16 Apr, 2015 07:45:01 Top
Bert Sinnema


Guest


Hello Andrei,

I'll have a look at the PDF later today and and let you know what the outcome is. Thank you!

Best,
Posted 16 Apr, 2015 09:55:21 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Sure. Please let me know if you run into any problem.


Andrei Smolin
Add-in Express Team Leader
Posted 16 Apr, 2015 09:56:25 Top
Bert Sinnema


Guest


I guess I'm missing a kind of example here. So I hooked up the PropertyChange event but I have no idea what to do with it in my particular challenge. Here is my code now:


        private void adxRibbonButton1_OnClick(object sender, IRibbonControl control, bool pressed)
        {
            if (pressed){
                // Button is Toggled: ON

                // I do some stuff here (logic)

            }else{
                // Button is Toggled: OFF

                // And I do some stuff here as well (logic)
            }
        }


        private void adxRibbonButton1_PropertyChanging(object sender, ADXRibbonPropertyChangingEventArgs e)
        {
            // And then somehow this should make my toggle button become Inspector independent ?
        }
Posted 16 Apr, 2015 15:37:36 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Hello Bert,

In the event handler, you retrieve the context (see section Determining a Ribbon Control's Context); the context is an Inspector object if the Ribbon's button Ribbons property is set to OutlookMailComoose (or OutlookMailRead).

Then you retrieve the property that you use to identify the inspector and calculate the condition. For example, you can

Then you return a value (see e.Value) depending on the condition and the property the Ribbon is requesting (see e.PropertyType). You may find useful the code example given in section Updating Ribbon Controls at Run Time.

Does this help?


Andrei Smolin
Add-in Express Team Leader
Posted 17 Apr, 2015 02:52:58 Top
Bert Sinnema


Guest


Hi Andrei,

This is so different than I'm used to. Could you give me an example based on my code? I really don't get it. From the documentation I can't really see the process flow or how it would apply to my challenge.

Best,
Posted 17 Apr, 2015 03:43:30 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Bert,

A raw sketch:

private void OnPropertyChanging(object sender, ADXRibbonPropertyChangingEventArgs e) {
    Outlook.Inspector inspector = e.Context as Outlook.Inspector;
    if (inspector == null) return;
    bool condition = inspector.Subject.Contains("some string");
    switch (e.PropertyType) {
        case ADXRibbonControlPropertyType.Caption:
            if (condition) {
                e.Value = "Some caption";
            } else {
                e.Value = "Some other caption";
            }
            break;
        case ADXRibbonControlPropertyType.Enabled:
            if (condition) {
                e.Value = true;
            } else {
                e.Value = false;
            }
            break;
    }
}


You can use the inspector window's handle to identify it: cast Outlook.Inspector to IOleWindow (http://pinvoke.net/default.aspx/Interfaces/IOleWindow.html) and retrieve the inspector window's handle using IOleWindow.GetWindow().

Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 17 Apr, 2015 04:24:15 Top
Bert Sinnema


Guest


Hi Andrei,

Let me just say I really appreciate the effort. I already starting to feel like a pain :).

Anyway, I think we're not completely on the same page. In your example there is a condition that looks for a string in a subject. I don't think I want to use that as a condition for a toggle button. I really can't understand this logic.

All I want is basically a seperate instance of the toggle button on each inspector. Totally individual, or at least that it seems that way.

http://snag.gy/aVe34.jpg

Is there no way to set a local variable inside each inspector to bind the state of the button in that specific inspector?

Best,
Posted 17 Apr, 2015 04:56:49 Top
Andrei Smolin


Add-in Express team


Posts: 18825
Joined: 2006-05-11
Bert,

Bert Sinnema writes:
In your example there is a condition that looks for a string in a subject. I don't think I want to use that as a condition for a toggle button.


That was a sample condition in a sample code. You've missed my suggestion to use a different condition based on the inspector window's handle.

Bert Sinnema writes:
All I want is basically a seperate instance of the toggle button on each inspector.


There's no "instances" of your button. What you see on the inspector window is what Outlook draws basing on the information that your code provides in response to Ribbon callbacks. Normally, the component responds to the callbacks for you; when handling the PropertyChanging event, you directly connect to the callbacks. When the user opens an inspector window and switches to the custom tab that your add-in creates, the following things occur:
- Office invokes a callback asking for the caption of the ribbon button
- Add-in Express gets the call and invokes the PropertyChanging event
- as demonstrated in the sample code, your add-in identifies the context (the inspector window in which Outlook is drawing the Ribbon button) and supplies a value basing on the context;
- Add-in Express passes the value to Office
- Office caches the text

In the same fashion Office finds out whether you want the button to be visible, enabled, etc. When all the information is retrieved, Office draws the button.

The same occurs when you open a new inspector. When you switch to the first inspector. Etc.

Bert Sinnema writes:
All I want is basically a seperate instance of the toggle button on each inspector. Totally individual, or at least that it seems that way.


You can write an inspector wrapper class so that the wrapper contains the information about the button shown in the inspector instance associated with the given instance of the wrapper class. When the Ribbon invokes the callbacks mentioned above, you will need to perform the following steps (for each property type - Visible, Enables, caption, etc):
- identify the inspector
- retrieve the corresponding wrapper (I suppose you store the wrappers in a dictionary, the key is the inspector window's handle, the value is the wrapper itself)
- retrieve the value of the corresponding property of the ribbon button wrapper class that you also need to create
- return the value retrieved.

Bert Sinnema writes:
Is there no way to set a local variable inside each inspector to bind the state of the button in that specific inspector?


This is possible with the inspector wrapper above; this is impossible with the Outlook.Inspector object.


Andrei Smolin
Add-in Express Team Leader
Posted 17 Apr, 2015 05:40:14 Top