Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to use FindComponent?

Status
Not open for further replies.

Kudel

Programmer
Dec 3, 2002
36
NO
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
 
do you get any error?
or what kind of messages do you get?

:eek:)

Michael
 
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
 
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;
 
I think bbegley and awarnica are right about this.. smile. they know their stoff..

michael
 
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
 
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
 
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
 
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
 
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...

 
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
 
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.
Code:
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;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top