Receiving a string as a component name?

Hi all,

so with the code:
~
begin...
  if Sender = Somename
    then do something
  else
    do something else;
~

I can check if the component name is "Somename" or so.
It uses the FindComponent(); function.

The question is: is there any other way to determine the NAME of the component trigering some procedure?
(Assuming I have more than 500 components to check, doing it the above way for every one will be real pain in the ass)...

Thx,
Miro.
firstmiroAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
not sure for what you are after
you could do something like this

if (sender is TEdit) then
  TEdit(sender).text := 'I''m triggered';

explain more about your problem

meikl ;-)
0
kretzschmarCommented:
or if you want the name of the sender?

if (sender is TComponent) then
  TheNameIs := TComponent(sender).Name;

really not sure what you need

meikl ;-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
firstmiroAuthor Commented:
Oh, my!
The question should be:
"Receiving a component name as a string..."

I'm populating dynamically a lot of checkboxes, but then, I need to know the name of a checkbox which fired a procedure.
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

firstmiroAuthor Commented:
meikl,

your solution:
if (sender is TComponent) then
 TheNameIs := TComponent(sender).Name;

is returning the class name of the component.
What I need it the Name property of that component.
(like TButton.Name := 'what I am looking for')

Miro.
0
geobulCommented:
Hi,

kretzschmar's code is the solution. Example: for a TButton component named Button1, TheNameIs variable contains 'Button1'.

Regards, Geo
0
kretzschmarCommented:
>...is returning the class name of the component.

no, thats not the case here,
you get the name you give in the object-inspector,
whereas tcomponent is the first ascendor which introduced
the name property, thats why i used this cast

just test it

meikl ;-)
0
ILECommented:
if you are using just edit box this is a solution for you

var s:string;
begin
  s:=(sender as tcheckbox).name;
 label1.caption:=s;
end;

if s u will recive name of the check box if i anderstand what u wont
another thing what u cen do


when u create dinamicly components use tag property to sppedup teh search

tab is a integer and u mey give tag any integer value u need   and test the tag propertis

u mey give on oncreate event define tag propertis of eny component like  1,2,3 ......
and then u cen use

 if (sender as tcheckbox).tag=123 then bla bla

or even do something like

case (sender as tcheckboh).tag of
  1..5:begin   bla, bla bla  end;
  6..30:begin   bla, bla bala  end;
  31,37,45:begin  bla,bla bla end;
else begin
     bla
        blaaaa
     end;

u get a picture ?
:)))




0
kretzschmarCommented:
you could use it, if you want to compare, like

if (sender is TComponent) and
   (TComponent(sender).Name = 'what I am looking for') then
  //do your action

whereas 'what I am looking for' is not a valid name

meikl ;-)
 
0
kretzschmarCommented:
?
s:=(sender as tcheckbox).name;

without classchecking could cause
unwanted effects, if sender is not a tcheckbox

just as hint, ile

meikl ;-)
0
ILECommented:
i know but i think he said that the components is tcheck box

if it is not then



var s:string;
begin
 if (sender is tcheckbox) then s:=(sender as tcheckbox).name;
label1.caption:=s;
end;


or if u like more



if sender is tcomponent then
begin
{this stuf with tag property what i said before}

end;


0
firstmiroAuthor Commented:
if (sender is TComponent) then
 TheNameIs := TComponent(sender).Name;

is the answer. True.

I just had hardcoded the output value. (yes, tired = stupid in my case).

Thank you kretzschmar! Thax guys!
0
DaFoxCommented:
firstmiro, if Meikl's comment was helpful you should PAQ your question. Click the button right next to his comment saying "Accept this comment as an answer".

Thanks in behalf of Meikl ;-)

Markus
0
firstmiroAuthor Commented:
Fast and absolutely accurate!
Thx mate!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.