Group:  Microsoft Outlook ยป microsoft.public.outlook.program_addins
Thread: Items in consecutive Selection collections not the same?

Geek News

Items in consecutive Selection collections not the same?
SeekerOfTruths 11/17/2008 5:12:09 PM
Hello.

I'm trying to write a VSTO add-in (my first venture in C# & VSTO) which will
allow me to construct wrapper objects around emails selected by the user (so
that I can hook into open events etc.) and I've run into an issue.

When a user selects multiple items I get a call into my SelectionChange
event hook for each item selected. On the first call the explorer.Selection
collection contains, one item, on the second call it contains two items, and
so on as one would expect.

Since I only want to create a single wrapped object for each selected item,
I had assumed that if I were to keep a copy of the previous selection and
then compare it with the current selection I would be able to determine
whether an item had been added to or removed from the selection, and identify
the object added or removed.

This doesn't seem to be the case however - if I compare each object in the
previous selection to each object in the current selection, I don't find any
matches, despite the fact that the second selection should (in theory)
contain all the objects in the first selection.

I can only assume that I'm making some sort of stupid mistake (see the code
below), because the idea that it's supposed to be that way (and that on each
selection change event I would have to destroy all my wrapped objects and
then re-create them again) just seems plain wrong.


void explorer_SelectionChange()
{
if (null != _priorSelection)
{
foreach (object oldItem in _priorSelection)
{
foreach (object newItem in _explorer.Selection)
{
if (oldItem.Equals(newItem))
{
System.Diagnostics.Trace.WriteLine("Match FOUND");
}
else
{
System.Diagnostics.Trace.WriteLine("No match FOUND");
}
}
}
}

_priorSelection = _explorer.Selection;
}

Any help in pointing out where I've gone wrong would be greatly appreciated!

SeekerOfTruths.
Re: Items in consecutive Selection collections not the same?
"Ken Slovak - [MVP - Outlook]" <kenslovak[ at ]mvps.org> 11/17/2008 7:22:08 PM
What version of Outlook?

You most likely will not be able to compare the items from the selections
directly using Equals or by comparing hash codes, although you can try hash
codes. Most likely you will have to empty your collection when selection
changes and reconstitute it.

--
Ken Slovak
[MVP - Outlook]
http://www.slovaktech.com
Author: Professional Programming Outlook 2007.
Reminder Manager, Extended Reminders, Attachment Options.
http://www.slovaktech.com/products.htm


"SeekerOfTruths" <SeekerOfTruths[ at ]discussions.microsoft.com> wrote in message
news:EACA7C92-88A2-4576-9547-B27B89525ED1[ at ]microsoft.com...
[Quoted Text]
> Hello.
>
> I'm trying to write a VSTO add-in (my first venture in C# & VSTO) which
> will
> allow me to construct wrapper objects around emails selected by the user
> (so
> that I can hook into open events etc.) and I've run into an issue.
>
> When a user selects multiple items I get a call into my SelectionChange
> event hook for each item selected. On the first call the
> explorer.Selection
> collection contains, one item, on the second call it contains two items,
> and
> so on as one would expect.
>
> Since I only want to create a single wrapped object for each selected
> item,
> I had assumed that if I were to keep a copy of the previous selection and
> then compare it with the current selection I would be able to determine
> whether an item had been added to or removed from the selection, and
> identify
> the object added or removed.
>
> This doesn't seem to be the case however - if I compare each object in the
> previous selection to each object in the current selection, I don't find
> any
> matches, despite the fact that the second selection should (in theory)
> contain all the objects in the first selection.
>
> I can only assume that I'm making some sort of stupid mistake (see the
> code
> below), because the idea that it's supposed to be that way (and that on
> each
> selection change event I would have to destroy all my wrapped objects and
> then re-create them again) just seems plain wrong.
>
>
> void explorer_SelectionChange()
> {
> if (null != _priorSelection)
> {
> foreach (object oldItem in _priorSelection)
> {
> foreach (object newItem in _explorer.Selection)
> {
> if (oldItem.Equals(newItem))
> {
> System.Diagnostics.Trace.WriteLine("Match FOUND");
> }
> else
> {
> System.Diagnostics.Trace.WriteLine("No match FOUND");
> }
> }
> }
> }
>
> _priorSelection = _explorer.Selection;
> }
>
> Any help in pointing out where I've gone wrong would be greatly
> appreciated!
>
> SeekerOfTruths.

Re: Items in consecutive Selection collections not the same?
SeekerOfTruths 11/18/2008 11:10:02 AM
Sorry I forgot to mention the Outlook version. It's 2007 SP1.

Thanks for the response though, even if the answer was the one I was hoping
I wouldn't hear!

It really does sound terribly inefficient to have to clear down my
collection of wrapper objects (and undo all their event hooks) each time the
selection changes and then rebuild it again based upon the new selection. If
I select 10 objects by holding down CTRL and clikcing on each object in turn
this will result in 10 calls to on selection change and I will have created
55 wrapper objects in total and destroyed 45 of them.

If the uderyling objects in the selection had stayed the same (perhaps they
are and I just can tell) then I could have just identified the difference
between selections and only created (or destroyed) objects as required.

Oh well, I'll give the hashcodes a go and if that doesn't work then I'll
just have to bite the bullet and go with the clear down and rebuild approach.

Thanks for the assistance though.

SeeketOfTruths.

"Ken Slovak - [MVP - Outlook]" wrote:

[Quoted Text]
> What version of Outlook?
>
> You most likely will not be able to compare the items from the selections
> directly using Equals or by comparing hash codes, although you can try hash
> codes. Most likely you will have to empty your collection when selection
> changes and reconstitute it.
>
> --
> Ken Slovak
> [MVP - Outlook]
> http://www.slovaktech.com
> Author: Professional Programming Outlook 2007.
> Reminder Manager, Extended Reminders, Attachment Options.
> http://www.slovaktech.com/products.htm
>
>
> "SeekerOfTruths" <SeekerOfTruths[ at ]discussions.microsoft.com> wrote in message
> news:EACA7C92-88A2-4576-9547-B27B89525ED1[ at ]microsoft.com...
> > Hello.
> >
> > I'm trying to write a VSTO add-in (my first venture in C# & VSTO) which
> > will
> > allow me to construct wrapper objects around emails selected by the user
> > (so
> > that I can hook into open events etc.) and I've run into an issue.
> >
> > When a user selects multiple items I get a call into my SelectionChange
> > event hook for each item selected. On the first call the
> > explorer.Selection
> > collection contains, one item, on the second call it contains two items,
> > and
> > so on as one would expect.
> >
> > Since I only want to create a single wrapped object for each selected
> > item,
> > I had assumed that if I were to keep a copy of the previous selection and
> > then compare it with the current selection I would be able to determine
> > whether an item had been added to or removed from the selection, and
> > identify
> > the object added or removed.
> >
> > This doesn't seem to be the case however - if I compare each object in the
> > previous selection to each object in the current selection, I don't find
> > any
> > matches, despite the fact that the second selection should (in theory)
> > contain all the objects in the first selection.
> >
> > I can only assume that I'm making some sort of stupid mistake (see the
> > code
> > below), because the idea that it's supposed to be that way (and that on
> > each
> > selection change event I would have to destroy all my wrapped objects and
> > then re-create them again) just seems plain wrong.
> >
> >
> > void explorer_SelectionChange()
> > {
> > if (null != _priorSelection)
> > {
> > foreach (object oldItem in _priorSelection)
> > {
> > foreach (object newItem in _explorer.Selection)
> > {
> > if (oldItem.Equals(newItem))
> > {
> > System.Diagnostics.Trace.WriteLine("Match FOUND");
> > }
> > else
> > {
> > System.Diagnostics.Trace.WriteLine("No match FOUND");
> > }
> > }
> > }
> > }
> >
> > _priorSelection = _explorer.Selection;
> > }
> >
> > Any help in pointing out where I've gone wrong would be greatly
> > appreciated!
> >
> > SeekerOfTruths.
>
>
Re: Items in consecutive Selection collections not the same?
"Dmitry Streblechenko" <dmitry[ at ]dimastr.com> 11/18/2008 5:30:04 PM
Compare based on the value of the EntryID property

--
Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
-
"SeekerOfTruths" <SeekerOfTruths[ at ]discussions.microsoft.com> wrote in message
news:EBC9B8E9-50D9-491B-A060-D5F3D9246FEE[ at ]microsoft.com...
[Quoted Text]
> Sorry I forgot to mention the Outlook version. It's 2007 SP1.
>
> Thanks for the response though, even if the answer was the one I was
> hoping
> I wouldn't hear!
>
> It really does sound terribly inefficient to have to clear down my
> collection of wrapper objects (and undo all their event hooks) each time
> the
> selection changes and then rebuild it again based upon the new selection.
> If
> I select 10 objects by holding down CTRL and clikcing on each object in
> turn
> this will result in 10 calls to on selection change and I will have
> created
> 55 wrapper objects in total and destroyed 45 of them.
>
> If the uderyling objects in the selection had stayed the same (perhaps
> they
> are and I just can tell) then I could have just identified the difference
> between selections and only created (or destroyed) objects as required.
>
> Oh well, I'll give the hashcodes a go and if that doesn't work then I'll
> just have to bite the bullet and go with the clear down and rebuild
> approach.
>
> Thanks for the assistance though.
>
> SeeketOfTruths.
>
> "Ken Slovak - [MVP - Outlook]" wrote:
>
>> What version of Outlook?
>>
>> You most likely will not be able to compare the items from the selections
>> directly using Equals or by comparing hash codes, although you can try
>> hash
>> codes. Most likely you will have to empty your collection when selection
>> changes and reconstitute it.
>>
>> --
>> Ken Slovak
>> [MVP - Outlook]
>> http://www.slovaktech.com
>> Author: Professional Programming Outlook 2007.
>> Reminder Manager, Extended Reminders, Attachment Options.
>> http://www.slovaktech.com/products.htm
>>
>>
>> "SeekerOfTruths" <SeekerOfTruths[ at ]discussions.microsoft.com> wrote in
>> message
>> news:EACA7C92-88A2-4576-9547-B27B89525ED1[ at ]microsoft.com...
>> > Hello.
>> >
>> > I'm trying to write a VSTO add-in (my first venture in C# & VSTO) which
>> > will
>> > allow me to construct wrapper objects around emails selected by the
>> > user
>> > (so
>> > that I can hook into open events etc.) and I've run into an issue.
>> >
>> > When a user selects multiple items I get a call into my SelectionChange
>> > event hook for each item selected. On the first call the
>> > explorer.Selection
>> > collection contains, one item, on the second call it contains two
>> > items,
>> > and
>> > so on as one would expect.
>> >
>> > Since I only want to create a single wrapped object for each selected
>> > item,
>> > I had assumed that if I were to keep a copy of the previous selection
>> > and
>> > then compare it with the current selection I would be able to determine
>> > whether an item had been added to or removed from the selection, and
>> > identify
>> > the object added or removed.
>> >
>> > This doesn't seem to be the case however - if I compare each object in
>> > the
>> > previous selection to each object in the current selection, I don't
>> > find
>> > any
>> > matches, despite the fact that the second selection should (in theory)
>> > contain all the objects in the first selection.
>> >
>> > I can only assume that I'm making some sort of stupid mistake (see the
>> > code
>> > below), because the idea that it's supposed to be that way (and that on
>> > each
>> > selection change event I would have to destroy all my wrapped objects
>> > and
>> > then re-create them again) just seems plain wrong.
>> >
>> >
>> > void explorer_SelectionChange()
>> > {
>> > if (null != _priorSelection)
>> > {
>> > foreach (object oldItem in _priorSelection)
>> > {
>> > foreach (object newItem in _explorer.Selection)
>> > {
>> > if (oldItem.Equals(newItem))
>> > {
>> > System.Diagnostics.Trace.WriteLine("Match FOUND");
>> > }
>> > else
>> > {
>> > System.Diagnostics.Trace.WriteLine("No match
>> > FOUND");
>> > }
>> > }
>> > }
>> > }
>> >
>> > _priorSelection = _explorer.Selection;
>> > }
>> >
>> > Any help in pointing out where I've gone wrong would be greatly
>> > appreciated!
>> >
>> > SeekerOfTruths.
>>
>>


Home | Search | Terms | Imprint Contact
Newsgroups Reader - provided by WiredBox.Net