Link to home
Start Free TrialLog in
Avatar of firstmiro
firstmiro

asked on

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.
Avatar of kretzschmar
kretzschmar
Flag of Germany image

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 ;-)
ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of firstmiro
firstmiro

ASKER

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.
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.
Hi,

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

Regards, Geo
>...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 ;-)
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 ?
:)))




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 ;-)
 
?
s:=(sender as tcheckbox).name;

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

just as hint, ile

meikl ;-)
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;


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!
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
Fast and absolutely accurate!
Thx mate!