Table of Contents
20.10 Watcher
This class provides a property and several events. It implements an object that can watch a passed component and raises some events when something happens to the watched object. Knowing what events are raised is useful when a component or container is moved, changed, displayed or hidden.
Minisini said on the subject of 'Watcher' that the Watcher class was developed well before the Observer (→ Chapter 20.18 Observer) and the latter class is therefore preferable. He emphasised that a Watcher object for a Form object would be nonsense because a Form already has Show(), Hide(), Move() and Resize() events because they are useful for windows, but not for buttons, for example. For example, a tray icon already has a Hide() and Show() event in the list of its properties and a Watcher object is therefore dispensable.
20.10.1 Property
The Watcher class has only one property. The property Watcher.Control of type Control returns the name of the watched component with Watcher.Control.Name.
20.10.2 Events
The Watcher class has four events, the description of which can be found in the next table:
| Event | Description |
|---|---|
| Hide | The event is triggered when the observed component is hidden. |
| Move | The event is triggered when the observed component is moved. |
| Resize | The event is triggered when the observed component changes its size. |
| Show | The event is triggered when the observed component is displayed. |
Table 20.10.2.1 : Events of the Watcher class
20.10.3 Project
The project shows the use of the four events and the property of the Watcher class and has more historical value, because for real projects you would resort to the Observer class.
' Gambas class file Private wWatcherObject1 As Watcher Private iBusy As Integer Private sControlName As String Public Sub Form_Open() FMain.Center() FMain.Resizable = True wWatcherObject1 = New Watcher(FMain) As "wWatcher" sControlName = "'" & wWatcherObject1.Control.Name & "'" End Public Sub wWatcher_Show() Message.Info("The main window " & sControlName & " is displayed!") Stop Event End Public Sub wWatcher_Hide() Message.Info("The main window " & sControlName & " has been hidden!")) Stop Event End Public Sub wWatcher_Resize() Inc iBusy If iBusy = 1 Then Message.Info("The main window " & sControlName & " has changed its size!") Dec iBusy Stop Event End Public Sub wWatcher_Move() Inc iBusy If iBusy = 1 Then Message.Info("The main window " & sControlName & " has changed its position!") Dec iBusy Stop Event End Public Sub btnFormMove_Click() FMain.X = FMain.X + 100 End Public Sub btnFormResize_Click() FMain.H += 50 FMain.W += (1.333 * 50) End Public Sub btnFormMin_Click() If FMain.Visible Then FMain.Minimized = True End Public Sub btnFormMax_Click() FMain.Maximized = True End Public Sub btnClose_Click() FMain.Close End
Figure 20.10.3.1: Project: Demonstration Watcher
Notes:
- Even though the Form component already has the Show(), Hide(), Move() and Resize() events in its class definition, the Form component was ideal for demonstrating the use of the Watcher class.
- You can also manually enlarge the programme window using the handles on the sides and in the corners or move the programme window, because each event is acknowledged with the display of a MessageBox.
- Each displayed MessageBox must be closed before you can continue working.
- Move the programme window or change the size of the programme window also via the 4 buttons or with the three buttons in the window line or with an elegant double-click in the window line.
You can find the complete Project 'Watcher' in the download area.

