Solved

Focus change with TCustomControl

Posted on 2002-07-03
3
639 Views
Last Modified: 2007-12-19
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
Comment
Question by:Zoly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
TOndrej earned 150 total points
ID: 7127389
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
 
LVL 8

Expert Comment

by:TOndrej
ID: 7127400
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
 

Author Comment

by:Zoly
ID: 7127633
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question