Solved

onExit function get name of component that called it

Posted on 2006-07-12
6
223 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
[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
  • 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

Industry Leaders: 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!

Question has a verified solution.

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

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 The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

688 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