Solved

onExit function get name of component that called it

Posted on 2006-07-12
6
215 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

733 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