[Gambas-user] Form listener

Tobias Boege taboege at ...626...
Mon May 29 17:34:41 CEST 2017


On Mon, 29 May 2017, Leon Davis wrote:
> Using Gambas v3.9.2 and GTK+3
> 
> I have added a form to another form:
> 
> Dim NForm as Form
> NForm = new MyForm(ME) as "MyForm"
> 
> The problem I'm having is none of the Key events, i.e. (keypress, keyup,
> keydown) for either form do anything. Also the Form_Resize for NForm
> doesn't fire. In my search I found an item that mentioned doing a search
> for listener in the Gambas wiki but that search was unsuccessful. Searched
> the mailing list with the same result. Any suggestions?

What is your problem exactly; where does the event not fire? But let me make
a guess to what question you are asking and answer that question.

You want to know why the Form_Resize event in your MyForm class does not
fire? The reason for this is that you give your NForm instance of the MyForm
class an event name "MyForm" explicitly (by using As "MyForm").

I'll assume that the above code is executed inside another Form called, say,
FParent. If you create an object inside FParent and give it an event name,
as you do above, FParent will become the *default event observer* for that
object. It will intercept all the events that the object NForm raises and
these events will be issued under the event name "MyForm" you specified.

Why is this a problem? Normally, forms are their own default event observer,
under the event name "Form". This is a mechanic hidden inside the Form class
code -- when a form object is created and detects that it hasn't gotten an
explicit default event observer, it makes itself its own event observer.
This is what enables you to normally intercept a Form's Resize event inside
its own class code, by defining the Form_Resize event handler.

To summarise: because you use the As "MyForm" syntax, you give NForm a new
default event observer (namely FParent). You can now intercept the events of
NForm inside the code of FParent, by using the event name MyForm, but you
*cannot* intercept the events in the MyForm code using the event name Form
anymore.

The solution is: don't use the As "MyForm" syntax if you want to continue
receiving NForm's events inside MyForm's class code. Instead you should
*duplicate* all the events by using an Observer:

  Dim NForm As MyForm
  Dim hObs As Observer

  NForm = New MyForm
  hObs = New Observer(NForm) As "MyForm"

Now you have NForm which is its default event observer (so Form_Resize will
fire inside the MyForm class) and you duplicate all the events that happen
through the observer, so you also receive the events in FParent, such as
MyForm_Resize. I believe this is what you mean by "listener", but the
appropriate Gambas term is "observer".

The article "The Gambas Object Model" [1] in the wiki should be mandatory
reading for everyone. It feels like I spend more time citing this page than
anything else I do on this mailing list.

Regards,
Tobi

[1] http://gambaswiki.org/wiki/doc/object-model#t10

-- 
"There's an old saying: Don't change anything... ever!" -- Mr. Monk




More information about the User mailing list