In the program I have multiple panels being created with picture boxes. The picture boxes are delegated to be clickable. I would like to have the user to have the option to remove one of the panels/picture boxes from the order. Right now if it is removed and the order is rearranged all picture boxes after the one that was removed retains its delegated order. So clicking on any of them after the one that was removed, skips to the one next to it down the line (ie click on #9 and it will go to #10). I need to remove the delegation of the reordered ones and re delegate them correctly. I have tried:
int z2 = z;
var myClickDelegate = (EventHandler)delegate { clicked(z2, null); };
PicBx[z].Click += myClickDelegate;
to create and
PicBx[z].Click -= myClickDelegate;
to remove
and also
int z2 = z;
PicBx[z].Click -= delegate { clicked(z2, null); };
but both of them does not remove the origional delegation.
Your first method should work, but the later one shouldn’t work.
because when you do
PicBx[z].Click -= delegate { clicked(z2, null); };method you are not removing the old delegate, instead you are creating a new delegate and then removing it.In your first try this should work:
Edit: Pair to your comment:
I notice that you are only in your delegate adding
clicked(z2, null), so I assumed that you are only creating the delegate at the first place just to pass that intz2to represent the picture box index.You can put that index with the picture box itself by using
pictureBox.Tagand in the click event get thatintfrom the tag:And so in the clicked event:
Edit2: As pair to your comments, it seems like you have a different signature of the
clickedmethod:Here we only change it to be: