[Gambas-user] Catching events from dynamic control form
Benoit Minisini
gambas at ...1...
Tue Dec 9 20:59:25 CET 2003
On Tuesday 09 December 2003 01:37, Jake Stookey wrote:
> Mandrake Linux 9.2
> Gambas Version 0.72 built from source
>
> Hello,
> I recently started playing with Gambas and it's
> really wonderful (from a VB 6 user).
I like these kind of remarks.
>
> I have one question. I would like to be able to
> capture an event raised by a control form that I have
> dynamically added to my main form (using NEW).
>
> I was able to dynamically add a button control to
> the main form and capture events from the button by
> declaring the button -- AS "MyButton" -- and captuting
> the event in -- SUB MyButton_Click --. However, I was
> unable to recreate this functionality in a control
> form that I created myself.
>
> Here are the few lines of code that demonstrate my
> problem.
>
>
> My main form starts out completely blank, so that I
> can add my control to it:
>
> ' ---------- Fmain form -------------
> PRIVATE FormControl AS Fsub
>
> PUBLIC SUB Form_Open()
> ' Add control form Fsub as a control on form Fmain
> at run-time
> FormControl = NEW Fsub(ME) AS "SubForm"
> FormControl.Left = 0
> FormControl.Top = 0
> END
>
> PUBLIC SUB SubForm_MyEvent()
> Message("Hello!")
> END
>
> The Fsub form has a single button on it called button1
> ' ---------- Fsub control form -------------
> ' Fsub form
> EVENT MyEvent()
>
> PUBLIC SUB Button1_Click()
> RAISE MyEvent()
> END
>
> The problem is that Fmain never captures the MyEvent.
> Can someone tell me what I am doing wrong?
>
> Thank you,
> Jake Stookey
>
> PS: You can download my small project from here if
> that's easier:
>
> http://www.jstookey.com/eventtest.tar.gz
>
Embedded forms are not exactly like controls, as the events they generate must
be processed inside the form code. In other words, embedded forms, like
forms, keep being their own event observer.
A workaround is using Object.Attach() to change the embedded form event
observer, this way:
FormControl = NEW Fsub(ME) AS "SubForm"
Object.Attach(FormControl, ME)
But be careful ! The events generated by FormControl are mixed with the events
generated by ME, and the events generated by the controls of FormControl keep
being handled by FormControl. And you may create circular references this
way...
Regards,
--
Benoit Minisini
mailto:gambas at ...1...
More information about the User
mailing list