User Tools

Site Tools


k20:k20.5:start

20.5 Observer

The Observer (gb) class implements an object that can observe another object by intercepting its events.

20.5.1 Properties

The Observer class has these two properties - Object and Tag.

  • The distinctive property Observer.Object of type Object returns the observed object.
  • The Tag property can be read or set. The use of this property is at the discretion of the programmer.

20.5.2 Generating an Observer Object

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
  • ObjectA is the object to be observed.
  • Normally, an observer gets the event of an object before its default observer. If you set the optional parameter After to False (default), then it is possible to intercept all events from ObjectA before they are actually triggered. This opens up the possibility that the observer can also optionally cancel an event to avoid an object triggering an event in the first place.
  • However, if you set the optional parameter After to True, the Observer will catch events of the observed object after they have been processed. In this case, you will no longer be able to evaluate or cancel events of the object being observed, for example.
  • For each intercepted event, the Observer object will trigger an event with the prefix EventName_ with the same name and arguments → Project 1, 2 and 3.
  • The created Observer object hObserver is bound to the object to be observed and is only released when the observed object is released.

20.5.3 Project 1

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:

B1

Figure 20.5.3.1: Temporary observation of Button1.

B2

Figure 20.5.3.2: Observation of Button1 has been switched off.

  • The source code has been written to temporarily observe the click event of Button1 (button text 'Determine observation status').
  • After the 3rd click on Button1, the observation is switched off because then the triggered event is no longer cancelled.
  • Therefore, you can then see the output in the Button1_Click event in the console of the IDE.
' 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.

20.5.4 Project 2

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

20.5.5 Project 3

The following special features characterise the 3rd project:

  • All controls - including those that are not visible - are observed in the programme window.
  • Only the Enter event is observed.
  • A separate observer is created for each control.
  • A common event group name (ObservedAll) is assigned for all observers.
  • To distinguish the controls of the same group in the event handler, each control has a special value for the tag property.

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

B3

Figure 20.5.5.1: Project 3 - GUI

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k20/k20.5/start.txt · Last modified: 22.10.2023 by emma

Page Tools