Solved

FindComponent() across forms

Posted on 2003-10-24
2
5,649 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

810 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