Multiple AddHandler how to manage all RemoveHandler?

I have a WebDialog which I am using as a confirmation message with ‘Save’ and ‘Don’t Save’ buttons. I would like to handle each case when the end user clicks either button.

When instantiating the WebDialog (in the Pressed event of a button), I run the following:

Session.WarningDialog = New ConfirmationWebDialog("Save changes?", "Do you want to save changes to all input fields?")
AddHandler Session.WarningDialog.DontSaveButton.Pressed, AddressOf ClearAllHandler
AddHandler Session.WarningDialog.SaveButton.Pressed, AddressOf SaveHandler
Session.WarningDialog.Show

Since both Addhandler are created before the end user even clicks an option, where is best to RemoveHandler? Is it best to remove both handlers in each of the methods (ClearAllHandler and SaveHandler).

I am just wondering what is best practice since if I had many more AddHandler, I would need to copy all RemoveHandler commands in every hander functions. This approach seems like it’s prone to forgetting to RemoveHandler causing object leak. Is there a better way to approach this?

Why not add some properties to the Webdialog and set them according to the actions the user has taken. Then when the dialog is closed you have only one single Handler metod where you can check what properties/flags have been set and act accordingly?

could be the Dismissed event a better place for RemoveHandler?

1 Like

Not to mention that handling the Pressed event will probably cause the code to not compile since I asume there is already some code in the button’s pressed event handler

Thanks. Good points about setting properties and/or putting RemoveHandler in Dismissed event.

I have removed the Pressed event from the buttons and just depending on my handler methods.