[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 658
  • Last Modified:

Focus change with TCustomControl

Hi,

I have a problem with a set of visual components I'm developing. I'm sure it's something basic, but I'm pretty new to component design so please humor me if I'm asking something really stupid.

My controls are all descendants of TCustomControl and for the most part they work perfectly, with one little exception. They don't receive focus when I press the TAB key. The TabStop value is true, everything (as far as I know) is set up correctly but still, they just don't receive focus with the TAB key. (Clicking on them with the mouse does work, of course.)

Please help.

Z
0
Zoly
Asked:
Zoly
  • 2
1 Solution
 
TOndrejCommented:
TabStop is set to False by default in TWinControl constructor. Do you set it to True in your overriden constructor?
Also if you want to reflect focus state change you need to write code to do that.

Maybe this quick example will help you:

unit Unit2;

interface

uses
  Windows, Classes, Messages, Graphics, Controls, Forms;

type
  TTestControl = class(TCustomControl)
  private
    procedure CMEnter(var Message: TCMEnter); message CM_ENTER;
    procedure CMExit(var Message: TCMExit); message CM_EXIT;
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
  end;

implementation

{ TTestControl }

procedure TTestControl.CMEnter(var Message: TCMEnter);
begin
  inherited;
  Invalidate;
end;

procedure TTestControl.CMExit(var Message: TCMExit);
begin
  inherited;
  Invalidate;
end;

procedure TTestControl.Paint;
const
  FocusColors: array[Boolean] of TColor = (clGreen, clRed);
begin
  with Canvas do
  begin
    Brush.Color := FocusColors[Focused];
    FillRect(ClientRect);
  end;
end;

constructor TTestControl.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  TabStop := True;
end;

end.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure FormCreate(Sender: TObject);
  private
  public
  end;

var
  Form1: TForm1;

implementation

uses
  Unit2;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  with TTestControl.Create(Self) do
  begin
    Parent := Self;
    Left := 8;
    Top := 8;
    Width := 120;
    Height := 48;
  end;
end;

end.

This seems to work as expected; the control does receive focus with Tab key and reflects the change by painting itself with a different color.

HTH
TOndrej
0
 
TOndrejCommented:
I forgot to change the declaration of TabStop property default value (which is advisable if you set your defaults in the constructor differently than the ancestor does):

type
  TTestControl = class(TCustomControl)
  ...
    property TabStop default True;
  end;
0
 
ZolyAuthor Commented:
Thanks! I handled every single message from WM_SETFOCUS to WM_DIALOGCHAR but it never occured to me to catch CM_ENTER and EXIT. :)

Z
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now