?
Solved

Focus change with TCustomControl

Posted on 2002-07-03
3
Medium Priority
?
644 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 600 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month7 days, 23 hours left to enroll

765 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