More popover problems

I have a simple popover class - taPopover - a desktopContainer containing a textArea. Its property .text copies the string into the text area. Changes to the textArea raise a TextChanged event.

A button on a window calls the popover and enters text into the popover text area. Clicking away from the popover closes (or hides, not sure) the popover. Changes in text in the popover are sent via an event handler back to the window calling the popover.

If I use this in the calculated property for the pop, then the textArea does get populated, but the app crashes on trying to open the pop again with a nil object - as the textArea no longer seems to be present.

Private Property pop As taPopover
Get
  If mPop = Nil Then
    mPop = New taPopOver
    AddHandler mPop.textChanged, AddressOf on_nameChanged
  End If
  return mpop
End Get

Set
End Set

End Property

If I use this in the calculated property, then the text does not get populated, but I can continually reopen the pop - even though it is blank.

Private Property pop As taPopover
Get
  'If mPop = Nil Then
  mPop = New taPopOver
  AddHandler mPop.textChanged, AddressOf on_nameChanged
  'End If
  
  Return mpop
End Get

Set
End Set

End Property

in both cases adding text to the textArea does trigger the event sending the text to the window, but the popover doesn’t work as expected in either.

How can I solve this?

You can’t continually re-use a PopOver.

Behind the scenes, a popover is a window. Therefore once the user clicks away from it, the popover is closed by the OS and thus window used is Nil.

Documentation for ShowPopover

You’re also going to leak handlers like mad since you never get the chance to RemoveHandler before the window is closed (and the controls on it destroyed). A less frictional design would be to use a Delegate to communicate the live-text updates.

Can a reference to the object in the calling window be passed to the popover, and the popover make changes to it?
I’m trying to adapt the solution from this post to get info from a popover:

Thanks, I’ve implemented a delegate instead of addHandlers (phew, delegates need a little thinking).

For reference, I put the property for the delegate inside the popover container and created a new popover each time it is called. Code below:

Private Property pop As taPopover
Get
  mPop = New taPopOver
  mPop.Text = TextField1.Text
  mPop.on_nameChanged_delegate = AddressOf on_nameChanged
  
  Return mpop
  
  //notes
  //mPop.on_nameChanged_delegate as del_on_NameChanged
  //delegate del_on_nameChanged(s as string)
End Get

Set
End Set

End Property