Solved

FindComponent() across forms

Posted on 2003-10-24
2
5,623 Views
Last Modified: 2012-06-27
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.
0
Comment
Question by:Kapusta
2 Comments
 
LVL 8

Accepted Solution

by:
gmayo earned 75 total points
ID: 9617344
Perhaps you need to call it like this:
for Ctr := 1 to 4 do begin
 if (Form1.FindComponent('Edit' + IntToStr(Ctr)) as TEdit).Text = '' then exit;
end;

...ie use the Form1's FindComponent rather than Form2's FindComponent (which presumably would only search its own components).

Geoff M.
0
 

Author Comment

by:Kapusta
ID: 9617643
That solved the problem!  Much thanks.  :-)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now