Solved

Focus change with TCustomControl

Posted on 2002-07-03
3
610 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
  • 2
3 Comments
 
LVL 8

Accepted Solution

by:
TOndrej earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now