Link to home
Start Free TrialLog in
Avatar of Kapusta
Kapusta

asked on

FindComponent() across forms

I'm having trouble with the FindComponent() function in Delphi 3.

I have 2 forms in my application, Form1 and Form2.  One form 1 I have an array of components that I want to access from Form2.

Here's sample code from Form2 trying to accessing the component  on Form1:

if Form1.Edit1.Text = '' then exit;

This works fine, and life is great.  However, I said that there was an array of components on Form1, and I need to cycle through them from Form2.  Example:

for Ctr := 1 to 4 do begin
  if Form1.Edit (insert Ctr's value here).Text = '' then exit;
end;

Of course the above code will not compile, but it gives you an idea of what I'm trying to do.

In the past, I typically have used the FindComponent function in such situations.  I  have tried the following code, but I keep getting "Access Violation" errors whenever program execution reaches this function:

for Ctr := 1 to 4 do begin
  if (FindComponent('Form1.Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

Why does the above trip an exception when the following code does not trip any exceptions?:

if Form1.Edit1.Text = '' then exit;
if Form1.Edit2.Text = '' then exit;
if Form1.Edit3.Text = '' then exit;
if Form1.Edit4.Text = '' then exit;

If I try using the problem code on Form1 (instead of Form2), then this code works fine:

for Ctr := 1 to 4 do begin
  if (FindComponent('Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

Any suggestions/ideas?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of gmayo
gmayo
Flag of United Kingdom of Great Britain and Northern Ireland 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 Kapusta
Kapusta

ASKER

That solved the problem!  Much thanks.  :-)