[Gambas-user] Forward events from constructor

Benoît Minisini gambas at ...1...
Sat Oct 3 02:26:13 CEST 2015


Le 03/10/2015 01:22, Tobias Boege a écrit :
> Hi,
>
> I'm puzzled by some behaviour of the interpreter when objects raise events
> from their constructors.
>
> The following big block of text just gives the rationale of my question. I
> think what I'm doing is not too strange. There is a short demonstration
> project attached.
>
> In my real project I want to implement a TextBox with a ToolButton next to
> it. This thing has a Validate event and should it be Stop Event'd by the
> user, this counts as invalid input. The validity of the input is reflected
> by the ToolButton's Picture. The user can specify a message which is dis-
> played as a Balloon over the ToolButton after the Button was clicked,
> explaining why the input is invalid.
>
> This is done in the following manner: FValidateBox is a Form class which
> handles all the logic. On top of that is ValidateBox.class which keeps an
> internal FValidateBox. If that FValidateBox raises its Validate event,
> ValidateBox forwards it to the user and redirects any Stop Event back to
> the FValidateBox to evaluate.
>
> This works, except that I also want to force an initial validation of the
> empty TextBox to initialise the ToolButton's Picture correctly (the question
> is here: does the user accept the empty string?). So I write in the
> constructor of ValidateBox.class:
>
>    Inherits UserControl
>
>    Private $frmControl As FValidateBox
>
>    Public Sub _new()
>      $frmControl = New FValidateBox(Me) As "Control"
>      ' Call the Change handler of the TextBox which initiates the validation
>      ' process by raising $frmControl's Validate event.
>      $frmControl.txtInput_Change()
>    End
>
> I noticed that in this scenario, the event raised by $frmControl does not
> reach the Control_Validate() handler in ValidateBox.class and therefore the
> validation process gets a false positive "correct" answer since nobody had
> a chance to Stop Event. [ As a workaround, I think I can use a Timer with
> short delay, despite its ugliness. But I'm curious about the reason. ]
>
> Is there a limitation, about raising events from constructors, I don't know
> of? Also note how, in the demonstration project, I can very well initiate
> the validation procedure just after the constructor finished.
>
> Regards,
> Tobi
>

The logic behind that behaviour is that an object whose constructor is 
not finished should not be able to raise or catch events.

Said differently, an object must be ready to deal with events.

Regards,

-- 
Benoît Minisini




More information about the User mailing list