CloneMenu

L

Lev Elbert

Hi, all!

Right to the topic:

In the main menu of the form I have View->Image-> and then 4 submenu
items (let's call them original submenu). I want the same submenu as a
Context menu. To do this I use such construct:

foreach (MenuItem mi in this.menuImage.MenuItems)
{
mi.Enabled = imageLoaded;
ctxtMenuView.MenuItems.Add(mi.Index, mi.CloneMenu());
}

All is kosher because CloneMenu does a deep copy.

In fact I create new items (by cloning). So if attributes of the
original submenu somehow are changed after cloned, too bad: changes are
not reflected on context menu.

To solve such "hypothetical" problem, I modified the line:
ctxtMenuView.MenuItems.Add(mi.Index, mi);

I understood that both MenuItems collection (original submenu and
context menu) are referencing the same MenuItem objects.

The result is disappointing. Addition of the MenuItem mi to the context
menu MenuItems collection REMOVES IT(!???) from the original submenu
MenuItems collection.

Why? Is it by design or I'm doing something wrong?

NB. The situation seams hypothetical, but with the spread of multi core
processors may hurt somebody.
 
C

Cor Ligthert [MVP]

Lev,

If you do a deep copy it certainly will not go, most copies with dotNet (the
exceptions I know direct from mind are the copyTo, copying a serialized
object or those in Adonet) are shallow copies and that is in my idea what
you need. Only the references need to be copied.

Just my idea of your problem,

Cor
 
T

Tom Spink

Lev said:
Hi, all!

Right to the topic:

In the main menu of the form I have View->Image-> and then 4 submenu
items (let's call them original submenu). I want the same submenu as a
Context menu. To do this I use such construct:

foreach (MenuItem mi in this.menuImage.MenuItems)
{
mi.Enabled = imageLoaded;
ctxtMenuView.MenuItems.Add(mi.Index, mi.CloneMenu());
}

All is kosher because CloneMenu does a deep copy.

In fact I create new items (by cloning). So if attributes of the
original submenu somehow are changed after cloned, too bad: changes are
not reflected on context menu.

To solve such "hypothetical" problem, I modified the line:
ctxtMenuView.MenuItems.Add(mi.Index, mi);

I understood that both MenuItems collection (original submenu and
context menu) are referencing the same MenuItem objects.

The result is disappointing. Addition of the MenuItem mi to the context
menu MenuItems collection REMOVES IT(!???) from the original submenu
MenuItems collection.

Why? Is it by design or I'm doing something wrong?

NB. The situation seams hypothetical, but with the spread of multi core
processors may hurt somebody.

Hi,

Yes! It's by design. However, to solve such a problem, you'd pass in
your 'this.menuImage' reference /as/ the context menu - you don't need to
do any copying.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top