ExplorerSelectionChange fires everytime when clicking on the same item

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

ExplorerSelectionChange fires everytime when clicking on the same item
 
developer_cp




Posts: 48
Joined: 2016-10-28
According to the description of ExplorerSelectionChange
Occurs when the user switches to a different item in a folder using the user interface (UI) or programatically.

But it fires everytime I click on the same item without changing to a different item nor refocusing the Outlook window..

Test case, create a dummy add-in express plugin, just add a hook to to ExplorerSelectionChange.

        this.adxOutlookEvents.ExplorerSelectionChange += new AddinExpress.MSO.ADXOlExplorer_EventHandler(adxOutlookEvents_ExplorerSelectionChange);
        ...
        void adxOutlookEvents_ExplorerSelectionChange(object sender, object explorer)
        {
            logger.Debug("adxOutlookEvents_ExplorerSelectionChange wtf");
        }


Bug?
Posted 08 Nov, 2016 10:29:05 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello,

This is how the Outlook object model behaves. You can't do nothing about that.

To detect this scenario, you can compare the EntryId property of the currently and previously selected items.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Nov, 2016 00:51:23 Top
developer_cp




Posts: 48
Joined: 2016-10-28
Just an update to whoever might be reading this.

Basically Outlook fires SelectionChange events twice and I bet that it is for when the item is DESELECTED and SELECTED.

I'm not exactly sure what's are their firing order since ADXSelectionChange event does not contains any arguments.
I bet ADX can be enhanced by parsing the actual Outlook event to find out the EntryId instead of just making a blank call.

Following Andrei Smolin's suggestion by internally keeping track of the EntryId (bad programming IMHO), it effectively turns the SelectionChange events into the following:
"Selected item event is fired first, follow by deselected item event second"

But in reality, if we parse the actual Outlook event in the right way, the deselected event should've fired first follow by selected, but since ADX doesn't parse it, we won't know which one comes first...


The code can be improve and probably reduce the needs of grabbing the ActiveExplorer and freeing it all the time if ADX library can determine the firing order (whether is deselect first or selected first) and provides the EntryId.


Suggestion to ADX team would be to introduce 2 new events, ADXItemSelected and ADXItemDeselected.



Here is a sample code for anyone who may face the same issue.

	static string currentEntryId = string.Empty;
	...
	Explorer explorer = CurrentApp.ActiveExplorer();
	Selection selection = (explorer as Explorer).Selection;
	if (selection.Count == 1)
	{
	    object item = selection[1];
	    if (item is MailItem)
	    {
	        string entryId = (item as MailItem).EntryID;
	        if (currentEntryId != entryId)
	        {
	            //logger.Debug("ADXSelectionChange (select)");
	        }
	        else
	        {
	            // Outlook fires SelectionChange event twice, once for when the
	            // previous item is deselected, and the other one for when
	            // the new item is being selected.
	            //logger.Debug("ADXSelectionChange (deselect)");
	        }

	        currentEntryId = entryId;
	    }

	    Marshal.ReleaseComObject(selection);
	    Marshal.ReleaseComObject(explorer);
	}


Code could be simplify if ADX introduces 2 new events. To just this...

void Test_ADXItemSelected(string entryId)
{
	// do your thing with entryId
}


Or better yet...

void Test_ADXItemSelected(object item)
{
	if (item is MailItem)
	{
		// do your thing
	}
}

void TestADXItemDeselected(object item)
{
	if (item is MailItem)
	{
		// do your thing
	}
}


The existing SelectionChange event should be enhance to this IMHO:

void Test_ADXSelectionChange(string entryId)
Posted 22 Nov, 2016 11:44:39 Top
Andrei Smolin


Add-in Express team


Posts: 18821
Joined: 2006-05-11
Hello Eric,

developer_cp writes:
Basically Outlook fires SelectionChange events twice and I bet that it is for when the item is DESELECTED and SELECTED.


This assumption may be correct. It also may be wrong. Anyway, the description of the Explorer.SelectionChange event (which is mapped to the ExplorerSelectionChange event in Add-in Express) doesn't provide any hint on why the event occurs twice in *some* Outlook versions; see https://msdn.microsoft.com/en-us/library/office/ff869813(v=office.15).aspx. Accordingly, this assumption may be a long shot and building a solution on it is risky.

At https://msdn.microsoft.com/EN-US/library/office/ff865014.aspx, they write:


Except for certain form events, your program cannot assume that events will occur in a particular order, even if they appear to be called in a consistent sequence. The order in which Outlook calls event handlers might change depending on other events that might occur, or the order might change in future versions of Outlook.



Andrei Smolin
Add-in Express Team Leader
Posted 23 Nov, 2016 03:42:20 Top