Solved

FindComponent() across forms

Posted on 2003-10-24
2
5,725 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

710 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