Solved

onExit function get name of component that called it

Posted on 2006-07-12
6
210 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
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…

816 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

9 Experts available now in Live!

Get 1:1 Help Now