I have a window (ParentWindow), which needs to open another window. The ParentWindow contains data that the second window manipulates. When the second window closes, it needs to go completely away and not exist or be referenced by anything else.
I tried two methods, neither of which really works, so I’m asking for advice on what’s the “correct” way to handle this.
My first method:
The ParentWindow has a property, MySecondWindow as SecondWindow. The SecondWindow has a weakref getter/setter (MyParentWnd) to ParentWindow so that it can access the data and controls in the parent window. There can be only ONE SecondWindow at any given time.
When the ParentWindow needs to open the second window, it does:
Self.MySecondWindow = New SecondWindow 'Self is the parent window
NewSecondWindow.MyParentWnd = Self 'This is the reference the second window uses to access the parent
Self.MySecondWindow.Show 'Show the second window
The problem with this is that when the second window closes, the parent still has a reference to it, even though the second window has no controls and isn’t visible. If the parent needs to open another second window, it can’t tell whether the second window still exists or not except by checking to see if Self.MySecondWindow has any controls.
My second method:
The ParentWindow has a weakref getter/setter (MySecondWindow), and the second window has a weakref to the parent.
When the ParentWindow needs to open the second window, it does:
if Self.MySecondWindow <>nil then
Self.MySecondWindow.Close
end if
Self.MySecondWindow = nil 'because just closing the window doesn't nil the reference to it
dim SW as new SecondWindow
Self.MySecondWindow = SW
Self.MySecondWindow.MyParentWnd = Self
SW.Show
The problem with this version is that when the user closes the second window, the parent’s weakref comes nil, but the second window may still exist, though not be visible or have controls.
Thoughts? Suggestions?