Solved

onExit function get name of component that called it

Posted on 2006-07-12
6
202 Views
Last Modified: 2011-09-20
I'm building a funtion for when my user exits an editbox I want this same function to be called for all of my editboxes.  This is probably easy to do but I don't know how to get the name of the component that's calling the function.  So if I exit editbox1 how do I get editbox1's name in my procedure.

ex.

procedure TForm1.EditFieldExit(Sender: TObject);
begin

    //figure out which edit box called this function
   //change properties of that specific editbox ie Color
end;
0
Comment
Question by:ICPooreman
  • 3
  • 2
6 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17090654
procedure TForm1.EditFieldExit(Sender: TObject);
begin

  if (Sender is TCustomEdit) then
  begin
     with TCustomEdit(Sender) do
        ShowMessage(Name);
  end;

end;

Regards,
Russell
0
 

Author Comment

by:ICPooreman
ID: 17090824
That's giving me the Name of the form, I'm looking into how to access and change properties of the editbox that's calling the procedure.

Your function shows the message 'form1' where as I want to be able to find and manipulate 'editbox1'.  Maybe I'm missing a simple step.
0
 
LVL 6

Accepted Solution

by:
House_of_Dexter earned 125 total points
ID: 17090857
 if (Sender is TEdit) then
  begin
    if TEdit(Sender).Name = 'Edit1' then TEdit(Sender).Font.Color  := clBlue else
    if TEdit(Sender).Name = 'Edit2' then TEdit(Sender).Font.Color  := clRed else
    if TEdit(Sender).Name = 'Edit3' then TEdit(Sender).Font.Color  := clGreen;
  end;
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 6

Expert Comment

by:House_of_Dexter
ID: 17090904
you probably forgot the   with TCustomEdit(Sender) do
        ShowMessage(Name);

thats one of the reasons I hate WITH...instead do this...
TCustomEdit(Sender).Name or

var
  a_CustomeEdit: TCustomEdit;

begin
  if (Sender is TCustomEdit) then
  begin
     a_CustomomEdit := Sender;
   ShowMessage( a_CustomomEdit .Name);
  end;

end;



0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17090911
It should not be returning the form's name (at least its not for me). You can change it to:

procedure TForm1.EditFieldExit(Sender: TObject);
begin

  if (Sender is TCustomEdit) then
  begin
     ShowMessage(TCustomEdit(Sender).Name);
  end;

end;

just to ensure that the .Name resolves to the edit control, but the OnExit event (exposed from TWinControl) always passes Self as the sender, so this should be working as long as you have the EditFieldExit procedure bound to all of the OnExit events for the edits. Full example below:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    procedure Edit1Exit(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Edit1Exit(Sender: TObject);
begin

  if (Sender is TCustomEdit) then
  begin
     ShowMessage(TCustomEdit(Sender).Name);
  end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

end.

-- dfm --
object Form1: TForm1
  Left = 353
  Top = 205
  Width = 406
  Height = 203
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 28
    Top = 28
    Width = 121
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
    OnExit = Edit1Exit
  end
  object Edit2: TEdit
    Left = 28
    Top = 60
    Width = 121
    Height = 21
    TabOrder = 1
    Text = 'Edit2'
    OnExit = Edit1Exit
  end
  object Edit3: TEdit
    Left = 28
    Top = 88
    Width = 121
    Height = 21
    TabOrder = 2
    Text = 'Edit3'
    OnExit = Edit1Exit
  end
  object Button1: TButton
    Left = 184
    Top = 24
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 3
  end
end



Russell

0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17090926

It also helps if people read what has been written.

Russell
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now