?
Solved

Receiving a string as a component name?

Posted on 2003-03-18
13
Medium Priority
?
136 Views
Last Modified: 2010-04-04
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.
0
Comment
Question by:firstmiro
[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
  • 5
  • 4
  • 2
  • +2
13 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8161988
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
 
LVL 27

Accepted Solution

by:
kretzschmar earned 300 total points
ID: 8162027
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
 

Author Comment

by:firstmiro
ID: 8162075
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:firstmiro
ID: 8162111
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
 
LVL 17

Expert Comment

by:geobul
ID: 8162310
Hi,

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

Regards, Geo
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8162332
>...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
 
LVL 3

Expert Comment

by:ILE
ID: 8162334
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8162361
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8162380
?
s:=(sender as tcheckbox).name;

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

just as hint, ile

meikl ;-)
0
 
LVL 3

Expert Comment

by:ILE
ID: 8162477
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
 

Author Comment

by:firstmiro
ID: 8162638
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
 
LVL 6

Expert Comment

by:DaFox
ID: 8164913
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
 

Author Comment

by:firstmiro
ID: 8170924
Fast and absolutely accurate!
Thx mate!
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

770 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