Solved

Focus change with TCustomControl

Posted on 2002-07-03
3
629 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

740 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