I am trying to add and then remove custom MouseClick event handlers to some Buttons. The controls on the window are being built at run-time. I can add my custom handler as per another answered question.
localBtn.MouseClick += new MouseEventHandler((se, e) => Move_MouseClick(se, e, center, type));
I read the other questions about remove EventHandlers, but they seem to geared to the 'standard' event handler. I finally got this to compile
object se=null; MouseEventArgs e=null;
localBtn.MouseClick -= new MouseEventHandler((se1, e1) => Move_MouseClick( se, e, center, type));
(Where center and type are passed in parameters.)
I do not know if this works or not...as I have run out of time for today....
I'm creating a grid (8x4) of buttons (maybe larger in the future), when one is clicked, the surrounding buttons 'change' into action buttons. Think of tic-tac-toe and the center button is the one clicked, all the others become 'active'. The custom handler is created with a reference to the 'center' button and then an enum indicating which of the 8 buttons then handler is for, top,left,right, topLeft, BottomRight, etc.. Once one of the 'active' buttons is pressed 'something happens' and the handlers need to be removed and the buttons will revert to their 'inactive' state. The buttons are derived from an object that has 'references' to all the other 'buttons' around it.
I read How to remove all event handlers from a control and C# removing an event handlerr.
Perhaps a better control is more suited to what I'm doing ?
[edit] I have tried the -= as shown above but that did not work. The control still had both event handlers attached. The program did not fail in anyway, so I'm not sure what the -= did? I guess nothing.