It seems that what you are actually looking for is to detect a change of focus. You want to detect when a control gets focus, and then when it loses focus. For that you should use the OnEnter
and OnExit
events. The OnClick
event is simply the wrong event for the task.
From the documentation for OnEnter
:
Use the OnEnter event handler to cause any special processing to occur
when a control becomes active.
The OnEnter event does not occur when switching between forms or
between another application and the application that includes the
control.
When switching between controls in separate container controls such as
the TPanel and the TGroupBox controls, an OnEnter event occurs for the
container before the OnEnter event of the contained control.
Similarly, an OnExit event of the container occurs after the OnExit
event of the control in a container when focus moves to another
control outside the container.
For example, consider a form with an OK button and a group box that
contains three radio buttons, where focus is currently on the OK
button. When the user clicks one of the radio buttons, an OnExit event
of the button occurs, followed by an OnEnter event on the group box,
and finally an OnEnter event on the radio button that was clicked. If
the user then clicks on the OK button, an OnExit event for the radio
button occurs followed by an OnExit event for the group box, and then
the button's OnEnter event occurs.