An app has one main window that never closes. From that main window, a push button opens a child window like this:
Dim winChild As New myWinChild
AddHandler winChild.OKBtn, AddressOf HandleWinChildOK
AddHandler winChild.CXBtn, AddressOf HandleWinChildCX
winChild.ShowWithin( self )
The window could be opened many times over the course of the app. Will continuously using AddHandler cause memory problems?
Is it necessary to use RemoveHandler for this case? If so, where?
Yes, you must use RemoveHandler. There “where” is a bit tricky with this sort of setup.
Alternate ways of doing it:
- Pass the master window to the child window and have the child window call the appropriate methods on the master.
- Set Shared Properties on the child window, use ShowModal to show the child, then check those properties after it closes.
There are probably other ways, and I urge you to choose one of them over your proposed scheme.
Thanks Kem. Just to be sure I understand you correctly… Are you suggesting ditching the AddHandler / RemoveHandler setup completely? Is there no way this kind of setup can use RemoveHandler?
Yes, and yes but I’m recommending against it. There is a place for AddHandler/RemoveHandler to be sure, but there are better, more easily maintained ways of doing what you’re trying to do.