Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does a TCustomControl receive input focus?

I am writing a grid control which I based on TCustomControl so I can handle all the structure, painting and navigation myself. The bit that I can't seem to figure out is this:

In my constructor, I set ControlStyle to this:

ControlStyle := ControlStyle + [csCaptureMouse, csClickEvents,
    csDoubleClicks, csNeedsBorderPaint, csPannable];

The idea being that if the control handles mouse events, I can do things like set selection etc. I noticed though that the control never receives the focus. I happen to have a TComboBox on the form and it is clearly focused when the form is created. No matter how many times I click within my grid, the focus stays on the combobox.

This of course has implications for my handling of keyboard events as well.

So the question is, how is it determined that the focus should shift to a control when you click on it?

like image 945
Cobus Kruger Avatar asked Dec 30 '22 15:12

Cobus Kruger


1 Answers

The CustomControl should call SetFocus on itself when it is clicked upon.

procedure TMyCustomControl.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  inherited;

  if not (csDesigning in ComponentState) and CanFocus then
    SetFocus;
like image 159
Lars Truijens Avatar answered Jan 28 '23 05:01

Lars Truijens