The Observer (gb) class implements an object that can observe another object by intercepting its events.
The Observer class has these two properties - Object and Tag.
The following statement generates a new Observer for the specified object:
Dim hObserver As Observer ... hObserver = New Observer ( ObjectA As Object [ , After As Boolean ] ) As EventName
In project 1, a selected event is temporarily observed by a button. Outputs in the console of the Gambas IDE support the understanding of this observation:
Figure 20.5.3.1: Temporary observation of Button1.
Figure 20.5.3.2: Observation of Button1 has been switched off.
' Gambas class file Public hObserver1 As Observer Public iCount As Integer = 1 Public Sub _new() hObserver1 = New Observer(Button1, False) As "ObservedButton1" End Public Sub Form_Open() FMain.Resizable = False MovieBox1.Border = Border.None MovieBox1.Alignment = Align.Center MovieBox1.Playing = True End Public Sub ObservedButton1_Click() If iCount <= 3 Then Print "Der Button wurde zum " & iCount & ". Mal angeklickt. Das Click-Ereignis wird jedoch verworfen!" Stop Event Endif If iCount = 4 Then hObserver1 = Null MovieBox1.Playing = False FMain.Text = "Ende der Beobachtung ..." Endif Inc iCount End Public Sub Button1_Click() ' Original Print "Der Observer für Button1 wurde nach 3 Beobachtungszyklen abgeschaltet." End Public Sub Form_Close() If hObserver1 Then hObserver1 = Null End Public Sub Button2_Click() FMain.Close End
These outputs result from testing project 1:
Der Button wurde zum 1. Mal angeklickt. Das Click-Ereignis wird jedoch verworfen! Der Button wurde zum 2. Mal angeklickt. Das Click-Ereignis wird jedoch verworfen! Der Button wurde zum 3. Mal angeklickt. Das Click-Ereignis wird jedoch verworfen! Der Observer für Button1 wurde nach 3 Beobachtungszyklen abgeschaltet.
In contrast to project 1, in project 2 the observation of an object is created in such a way that the observer intercepts the Button1_Click event after it has been processed. In the generated ObservedButton1_Click() event, a random number is emitted that was generated in the Button1_Click event and stored in the hObserver1.Tag property.
Source code:
' Gambas class file Public hObserver1 As Observer Public Sub _new() hObserver1 = New Observer(Button1, True) As "ObservedButton1" End Public Sub Form_Open() FMain.Center FMain.Resizable = False MovieBox1.Alignment = Align.Center MovieBox1.Border = Border.None MovieBox1.Playing = True End Public Sub ObservedButton1_Click() Dim sMessage As String sMessage = "Vor 0.2 Sekunden wurde die folgende Zahl im beobachteten Button1_Click-Ereignis erzeugt: " Print sMessage; hObserver1.Tag End Public Sub Button1_Click() Dim fNumber As Float Randomize fNumber = Rnd(-2.0, 2.01) hObserver1.Tag = Round(fNumber, -2) Wait 0.2 End Public Sub btnClose_Click() If hObserver1 Then hObserver1 = Null FMain.Close() End
These outputs show in the console of the Gambas IDE:
Vor 0.2 Sekunden wurde die folgende Zahl im beobachteten Button1_Click-Ereignis erzeugt: -0,87 Vor 0.2 Sekunden wurde die folgende Zahl im beobachteten Button1_Click-Ereignis erzeugt: 1,18 Vor 0.2 Sekunden wurde die folgende Zahl im beobachteten Button1_Click-Ereignis erzeugt: 0,33
The following special features characterise the 3rd project:
The complete project can be found in the download area. Only the relevant source code is presented here, in which the above-mentioned special features can be easily read:
... Public Sub _new() SetAllObservers(ME) End Private Sub SetAllObservers(hContainer As Container) Dim hObject As Object Dim hObserver As Observer For Each hObject In hContainer.Children hObserver = New Observer(hObject, False) As "ObservedAll" '-- Rekursiver Abstieg If hObject Is Container Then SetAllObservers(hObject) Next End Public Sub ObservedAll_Enter() Select Last.Tag Case "PB" Print Object.Type(Last); " : "; Last.Name; " ->> Höhe = "; Last.H; "px" Case "HBOX" Print Object.Type(Last); " : "; Last.Name; " ->> Spacing = "; Last.Spacing Case "OI" Print Object.Type(Last); " : "; Last.Name; " ->> Text = '"; Last.Text; " '" Case "PRE" Print Object.Type(Last); " : "; Last.Name; " ->> Aktiv? = "; Last.Enabled Case "NEXT" Print Object.Type(Last); " : "; Last.Name; " ->> Tag-Eigenschaft = "; Last.Tag Case "PS" Print Object.Type(Last); " : "; Last.Name; " ->> Expand? = "; Last.Expand Case "CLOSE" Print Object.Type(Last); " : "; Last.Name; " ->> Font = "; Last.Font.ToString() Case "S" Print Object.Type(Last); " : "; Last.Name; " ->> X = "; Last.X; "px" End Select End
These outputs show up, for example, in the console of the Gambas IDE when you move the mouse over the controls:
PictureBox : PictureBoxD ->> Höhe = 360px HBox : HBox1 ->> Spacing = True Button : btnOpenFileImage ->> Text = ' Bilder auswählen ' Button : btnPrevious ->> Aktiv? = False Button : btnNext ->> Tag-Eigenschaft = NEXT Panel : panSpace ->> Expand? = True Button : btnClose ->> Font = Ubuntu,11 Separator : Separator1 ->> X = 8px
Figure 20.5.5.1: Project 3 - GUI
Chapter & Projects