[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Get events from controls created by code.


On 1/3/25 05:39, System64 Development wrote:
Hi,

When controls are created in a module, not on the form, but then these are placed in a panel on the main form, what is the way to get the events on these controls on the form?

This example print the text when Textarea1 change but not when the code created TextArea change.

'FMain

Public Sub Button1_Click()
   Makecontrols(Panel1)
End
Public Sub Makecontrols(cn As Container)
   Dim ob As Panel
   ob = Auxiliary.Capsule(Panel1)
End
Public Sub Text_Change()
   Dim o As Object
   o = Last
   Print o.Text
End
Public Sub TextArea1_Change()
   Dim o As Object
   o = Last
   Print o.Text
End

[snip]

Despite the fact that the child Panel created in the Auxiliary module, is given FMain.Panel1 as its parent, the default event observer for that child Panel is the Auxiliary module itself (and as far as I know, that cannot be changed). If you move the Text_Change event handler to the module, the event will be handled.

If you want the event to *also* be handled in FMain, then you will need to attach an Observer to it in FMain:
https://gambaswiki.org/wiki/comp/gb/observer

There may be better ways of doing this as it may not be practical for many controls, but here is a quick example with a modified Makecontrols procedure (with "Public TextObserver As Observer" at the top of FMain):

[code]
Public Sub Makecontrols(cn As Container)

  Dim ob As Panel

  ob = Auxiliary.Capsule(Panel1)
  For Each ctrl As Control In ob.Children
    If ctrl.Name = "Text" Then TextObserver = New Observer(ctrl) As "Text"
  Next

End
[/code]



--
Lee

--- Gambas User List Netiquette [https://gambaswiki.org/wiki/doc/netiquette] ----
--- Gambas User List Archive [https://lists.gambas-basic.org/archive/user] ----


Follow-Ups:
Re: Get events from controls created by code.System64 Development <64xcode@xxxxxxxxx>
References:
Get events from controls created by code.System64 Development <64xcode@xxxxxxxxx>