×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

How to use FindComponent?

How to use FindComponent?

How to use FindComponent?

(OP)
Hi

myFunction are supposed to display two equal messages, but it doesn’t. Why???

myFunction(temp: TEdit) : string;
var
  Obj : TObject;
  E : TEdit;
begin
  Obj := FindComponent(temp.Name);
  E := (Obj as TEdit);
  messagedlg(temp.text,mtInformation,[mbOK],0);
  messagedlg(E.Text,mtInformation,[mbOK],0);
end;

I have multiple forms and "temp" is created at run-time.

-Kudel

RE: How to use FindComponent?

do you get any error?
or what kind of messages do you get?



Michael

RE: How to use FindComponent?

I think this is because you have cast the temp object as a TObject, which does not have a text property.  I think this would work:

myFunction(temp: TEdit) : string;
var
  E : TEdit;
begin
  E := (FindComponent(temp.Name) as TEdit);
  messagedlg(temp.text,mtInformation,[mbOK],0);
  messagedlg(E.Text,mtInformation,[mbOK],0);
end;

Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: How to use FindComponent?

or this:

var
  Obj : TObject;
  E : TEdit;
begin
  Obj := FindComponent(temp.Name);
  E := TEdit(Obj);
  messagedlg(temp.text,mtInformation,[mbOK],0);
  messagedlg(E.Text,mtInformation,[mbOK],0);
end;

RE: How to use FindComponent?

I think bbegley and awarnica are right about this.. smile. they know their stoff..

michael

RE: How to use FindComponent?

(OP)
Hi bbegley and awarnica.

It did not work. All the codes give me one message with temp.text and one message that are blank.
 
I don’t think it have any thing to do with me casting temp as an TObject, because FindComponent returns an TObject.

Do anyone have any other idea?

Kudel

RE: How to use FindComponent?

why shouldn't it work??

procedure TForm1.Button1Click(Sender: TObject);
begin
  test(Edit1);
end;


procedure TForm1.test(temp: TEdit);
var
  Obj : TObject;
  E : TEdit;
begin
  Obj := FindComponent(temp.Name);
   E := TEdit(Obj);
  messagedlg(temp.Name,mtInformation,[mbOK],0);
  messagedlg(E.Text,mtInformation,[mbOK],0);
end;

michael

RE: How to use FindComponent?

it worked when I tried it...

RE: How to use FindComponent?

Awarnica and I did not quite say the same thing.

Awarnica says this:
  Obj := FindComponent(temp.Name);
  E := TEdit(Obj);

I would still think that once you make temp a TObject, you lose the text property, and the value that it holds.  I would be suprised if it retained the text value after you had cast it as TObject.

I tested this, and it seems to work:
 E := (FindComponent(temp.Name) as TEdit);

/////////////////////
Kudel says:
I don’t think it have any thing to do with me casting temp as an TObject, because FindComponent returns an TObject.
/////////////////////

FindComponent actually retuns a TComponent, but it will return any descendant of TComponent.  If it returns a DBEdit, you will have access to it's datasource property, and if it returns a TEdit, you will have access to its text property.



Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: How to use FindComponent?

I tested Awarnica's method, and it worked for me too.  That'll teach me to guess instead of test.

So if they both work when Michael and I test, maybe there is some other factor preventing it from working for you.

Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: How to use FindComponent?

When you cast an undefined object back to the original object:

Obj := FindComponent(temp.Name);
  E := TEdit(Obj);

you by definition do not lose any properties, you just have to let delphi know what kind of object you have. You still keep all the properties and methods - if you didnt you would not be able to use the object at all!

whatever way you cast back, you are just letting delphi what to do with this object, this space in memory, what kind of object it should be looking for...

RE: How to use FindComponent?

I should have known that.  You are just pointing at the existing object, not creating a new one.

Brian
"There are 2 kinds of people in the world, those that divide people into two groups and those that don't.  I belong to the second group." - tag line I stole

RE: How to use FindComponent?

Pardon me for jumping in, but I was able to make the original code work by making a few changes.  If I understand correctly, you want to search all forms to find the specific TEdit.

function myFunction(temp: TEdit) : string;
var
  Obj : TObject;
  E : TEdit;
  i : integer;
begin
  for i := 0 to Application.ComponentCount - 1 do
    if Application.Components[i] is TForm then
      with Application.Components[i] as TForm do
        begin
          Obj := Application.Components[i].FindComponent(temp.Name);
          if Obj is TEdit then
            begin
              E := (Obj as TEdit);
              messagedlg(temp.text,mtInformation,[mbOK],0);
              messagedlg(E.Text,mtInformation,[mbOK],0);
            end;
        end;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
var
  t:TEdit;
begin
  t := TEdit.Create( self );
  t.Name := 'NewComponent';
  t.Text := 'New Text';
  myFunction( t );
end;

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close