Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Focused 2

Status
Not open for further replies.

MinalMaurice

Programmer
Apr 20, 2005
14
GB
Using Delphi 7 and wanting to know which box I'm working in, whether DBEdit or DBRichEdit etc. Tried Field.Focused, which compiles but says False even though I'm in that Box.

Any ideas?

Thanks in advance.
 
I've set up ActiveControl := Screen.ActiveControl; which works but how do I then say

if ActiveControl is Edit1 then

because that doesn't compile.
 

Not sure what exactly you are trying to do, but one of these should get you closer:
Code:
  if ActiveControl.Name = 'Edit1' then
or
Code:
  ActiveControl.ClassType.ClassName = 'TEdit' then
 

Or, more simply
Code:
   if ActiveControl is TEdit then
For example,
Code:
  if ActiveControl is TEdit then
    with ActiveControl as TEdit do
      begin
        //  TEdit stuff...
      end;
 
I think there might not be an answer - I want to apply a procedure set off from a button which will work whichever field I'm modifying.

The procedure
ShowMessage('Name is '+ Screen.ActiveControl.Name);
showed the button name not the field name I want the procedure to modify.

ActiveControl is TEdit then doesn't work

What next.
 
You could try storing the name of each control of interest in a string variable in its OnExit event. When the button is clicked check the value of this variable.

eg

var
LastControl : string;

procedure TForm1.Edit1Exit(Sender: TObject);
begin
LastControl := 'Edit1'
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
if LastControl = 'Edit1' then
etc

end;


Hope this helps.

 
Using Delphi 7 and wanting to know which box I'm working in, whether DBEdit or DBRichEdit etc.

Is It DB controls you are using?
Then try this:

if (Self.ActiveControl is TDBEdit) then
ShowMessage('Name is '+ (Self.ActiveControl as TDBEdit).DataField);

 

A couple of possibilities:

1. As earthandfire suggests, you can store the current component name in a variable. I would probably use the OnEnter event.

2. You could use a toolbar or a menu item instead of a TButton. clicking a button on a toolbar does not take focus away and the ActiveControl should still be pointing to the component you want.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top