Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Receiving a string as a component name?

Posted on 2003-03-18
13
Medium Priority
?
137 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
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Screencast - Getting to Know the Pipeline
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
Suggested Courses
Course of the Month14 days, 13 hours left to enroll

577 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