Re: [Gambas-user] Events not firing in dynamically instantiated forms.
Brought to you by:
gambas
|
From: T L. D. <t.l...@gm...> - 2014-11-27 19:37:00
|
Wow. That is a very good explanation. Thank you, Tobi! I did understand that "a form is by default its own event observer". But I guess I did not fully understand exactly what that meant. (And, yes, I have seen code like, "Public Sub Form_Open()", and always wondered why it did not say "Form1_Open()".) I thought, when I first asked, that it would be a simple answer. (I apologize to you, Stephen, for unintentionally hi-jacking your thread. I did think you were done with it.) Okay, so: 1. Should that warning actually say, "Unless ~Class~ is a [Form](/comp/gb.qt4/form), if you forget to specify the ~Name~ part, your object will never raise events!" 2. And, should this explanation be put on the Wiki somewhere? If so, where? -- Lee __________ "Artificial Intelligence is no match for natural stupidity." On 11/27/2014 01:47 PM, Tobias Boege wrote: >> Thanks Tobi. I understand that a form is by default its own event observer. >> > >> >What confuses me is that the warning seems to indicate that the "As >> >Name" part is required for the object to be able to raise events. >> > >> >Thinking that the warning should say, "*If you specify As* but forget to >> >specify the Name part ...", I took off just the Name part leaving "As" >> >at the end of the line. I then got, "Unexpected end of line". >> > >> >So, I guess I'm just confused as to what that warning is actually saying. >> > > OK, "As name" comes as a bundle. It makes your newly created object raise > events under the given name and makes the current class its event observer. > This is called "attaching" the object to its observer. "As" without a name > is a syntax error. > > This means that in a class Bob the lines > > Private h As Alice > > Public Sub _new() > h = New Alice As "Beth" > End > > create a new Alice in the variable "h" and assigns her the event name "Beth" > (just to show that the event name is totally free to choose). > > The current Bob where you create that Alice is now her event observer, that > is you can intercept the events of "h" in the code of Bob. Say, Alice raises > an event called "Send", you can write > > Public Sub Beth_Send() > ' This is called when the h object raises the Send event. > End > > in your Bob.class. Take your time to experiment if you're still insecure > here. (Or ask for a concrete example!) > > If you don't give "As name", the object will not be attached to any observer > and this will prevent it from raising events (actually it_can_ raise events > but nobody will hear it). > > But Form plays a special role. It will attach itself to itself automatically, > using the event name "Form". You have very likely already seen code like > > Public Sub Form_Open() > ' ... > End > > without worrying about it. Well, you don't need to worry about it because of > the automatic attaching logic that is built into the Form class. > > However, if you create a new form and explicitely give it an event name, the > procedure I described above kicks in. That is, the form is not attached to > itself as "Form" but to the object which creates the form, with the given > name (an object can have at most have one primary observer). Consequently, > Form_Open within the form code will no longer fire. That's what the thread > was originally about. > > Regards, > Tobi |